如何停止在 Android webview 中播放 youtube 视频? [英] How to stop youtube video playing in Android webview?

查看:37
本文介绍了如何停止在 Android webview 中播放 youtube 视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何停止在我的网络视图中播放的 YouTube 视频?当我点击后退按钮时,视频不会停止,而是在后台继续.

How can I stop a YouTube video which is played in my webview? When I click on the back button the video doesn't stop and instead continues in the background.

代码:

webView = (WebView) findViewById(R.id.webview); 
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(false); 
webView.getSettings().setSupportZoom(false);
webView.loadData(myUrl,"text/html", "utf-8");

推荐答案

请参阅以下关于 WebView 线程永不停止

本质上,您需要从您自己的 Activity 的 onPause 方法中调用 WebView 的 onPause 方法.

Essentially you'll need to call the WebView's onPause method from your own Activity's onPause method.

唯一的技巧是你不能直接调用 WebView 的 onPause 方法,因为它是隐藏的.因此,您需要通过反射间接调用它.以下代码应该可以帮助您开始设置自己的 Activity 的 onPause 方法:

The only trick with this is that you cannot call the WebView's onPause method directly because it is hidden. Therefore you will need to call it indirectly via reflection. The following code should get you started on setting up your own Activity's onPause method:

@Override
public void onPause() {
    super.onPause();

    try {
        Class.forName("android.webkit.WebView")
                .getMethod("onPause", (Class[]) null)
                            .invoke(webview, (Object[]) null);

    } catch(ClassNotFoundException cnfe) {
        ...
    } catch(NoSuchMethodException nsme) {
        ...
    } catch(InvocationTargetException ite) {
        ...
    } catch (IllegalAccessException iae) {
        ...
    }
}

请注意,上面 try 块中的变量webview"是该类的私有实例变量,并在 Activity 的 onCreate 方法中分配.

Note that the variable 'webview' in the try block above is a private instance variable for the class and is assigned to in the Activity's onCreate method.

这篇关于如何停止在 Android webview 中播放 youtube 视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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