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:
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?