如何使WebView在后台播放视频或音频?

编程入门 行业动态 更新时间:2024-10-26 09:32:05
本文介绍了如何使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("www.w3schools/html/html5_video.asp"); } }

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

Now, if a website, e. g. www.w3schools/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?

推荐答案

经过大量搜索,我发现此线程.我做了类似的事情:只需扩展WebView并覆盖onWindowVisibilityChanged.

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在后台播放视频或音频?

本文发布于:2023-07-11 12:03:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1093989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   音频   视频   WebView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!