如何使WebView在后台播放视频或音频? [英] How can I make WebView keep a video or audio playing in the background?

查看:613
本文介绍了如何使WebView在后台播放视频或音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设,我有以下基本应用程序:

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");
   }

}

现在,如果是网站,则e. G. https://www.w3schools.com/html/html5_video.asp 包括一个视频或音频(由于用户可以打开任何站点,URL对我来说是未知的),并且用户打开另一个应用程序或关闭屏幕后,视频或音频就停止了.

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.

但是,我希望它继续.它应该类似于Google Chrome浏览器所做的事情:

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

我已经看到了启动后台声音服务的解决方案,但是,如果我不知道视频/声音文件的URL,如何找到它?我需要注入一些JavaScript还是有更清洁的解决方案?

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?

更新:我可以通过 JavascriptInterface ,但通常是一个Blob(例如,在YouTube中).我该怎么办?

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?

更新2:如您所见,我找到了一个解决方案.但是,我不确定这是否是干净的解决方案.有没有人有更好的解决方案-可能像在Chrome中那样在通知栏中使用控件?

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?

推荐答案

经过大量搜索,我发现

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.

这篇关于如何使WebView在后台播放视频或音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆