How can I make WebView keep a video or audio playing in the background?

Anonymous

Let us suppose, I have the following basic application:

public class MainActivity extends AppCompatActivity {

   private WebView webView;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       webView = (WebView) findViewById(R.id.webview);
       webView.getSettings().setJavaScriptEnabled(true);

       webView.setWebViewClient(new WebViewClient(){

           @Override
           public boolean shouldOverrideUrlLoading(WebView view, String url){
               return true;
           }

       });

       webView.loadUrl("https://www.w3schools.com/html/html5_video.asp");
   }

}

Now, if a website, e. g. https://www.w3schools.com/html/html5_video.asp, includes a video or audio (the URL is unknown to me because the user can open any site) and the user opens another app or turns off the screen, the video or audio is stopped.

However, I would like it to continue. It should be similar to what Google Chrome does:

Videos continue playing in background when using Google Chrome

I have seen solutions, starting a background sound service, however, how can I find out the URL of the video/sound file if it is unknown to me? Do I need to inject some JavaScript or is there a cleaner solution?

UPDATE: I am able to get the video URL with JavascriptInterface, but it is often a blob (e. g. in YouTube). How can I proceed with that?

UPDATE 2: As you can see, I have found a solution. However, I am not sure whether this is a clean solution. Does anyone have a better solution – maybe with controls in the notification bar like in Chrome?

Anonymous

After searching a lot, I found this thread. I did something similar: Just extend WebView and override onWindowVisibilityChanged.

public class MediaWebView extends WebView {

   public MediaWebView(Context context) {
       super(context);
   }

   public MediaWebView(Context context, AttributeSet attrs) {
       super(context, attrs);
   }

   public MediaWebView(Context context, AttributeSet attrs, int defStyleAttr) {
       super(context, attrs, defStyleAttr);
   }

   @Override
   protected void onWindowVisibilityChanged(int visibility) {
       if (visibility != View.GONE) super.onWindowVisibilityChanged(View.VISIBLE);
   }

}

This way, the audio continues to play if the screen is locked or another app is opened. However, there will be no controls in the notification bar or on the lockscreen.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I stop video from playing in background chewie plugin

How can I prevent video recording with AVFoundation from interrupting any global audio that is currently playing (Swift)?

How can I access the audio samples for the upcoming 5+ seconds from a <video> as it's playing?

How to stop video playing in WebView

Objective-C: How to make AVPlayer keep playing in the background

How can I stop a background video playing every time the user returns to the home page?

How can I make the background dark while watching video?

While using Javafx for playing a video, how can i make a “rect”(Processing lib), for example, appear in front of it?

How do I make the background of a WebView transparent?

How to make `just_audio` not interrupt the playback of whatever audio is playing in the background in Flutter?

How to stop audio playing on websites loaded in webView?

In Powerpoint VBA, how do I make an embedded video start playing?

How can I enable video and audio codecs?

While I keep my wrist down, how to keep playing audio in iWatch app? - watchOS

How to make a html5 video poster image transparent to see the background image while video is not playing

Bootstrap Modal keep playing audio after video window close in Firefox

Xcode :How can I stop video in webview and still using webview

How can I let the video start playing at particular position?

How can I lock the keyboard and mouse when playing video

How can I update current time of the video while it's playing?

How can I save the currently playing video in VLC Media player?

How can I seamlessly change a playing video on a website?

Check if a WebView is playing a video

ffmpeg: How to synchronize audio from overlayed video when that video is playing

How can I hear sound playing while scrubbing the audio from an <audio> tag?

why is my third song not playing and how can I make it play?

How can i make the function keep going

MPMoviePlayerViewController is playing video in background

How to change background color of video bar after playing the video