如何阻止webview视频在后台运行?

时间:2013-11-21 20:31:28

标签: youtube android-webview html5-video

HTML5视频在后台打开并不会停止。我正在片段中打开webview。我尝试了以下代码:

public void onPause(){
    mWebView.onPause();

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

        } catch(ClassNotFoundException e) {
            e.printStackTrace();
        } catch(NoSuchMethodException e) {
            e.printStackTrace();
        } catch(InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

         super.onPause();

}

此代码停止声音,但媒体播放器仍然在后台运行。当我再次打开相同的片段时,应用程序崩溃:

V/MediaPlayer(1173): notify(100, -38, 0) callback on disconnected mediaplayer
W/dalvikvm(1173): threadid=1: thread exiting with uncaught exception (group=0x41212438)
E/AndroidRuntime(1173): FATAL EXCEPTION: main
E/AndroidRuntime(1173): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
E/AndroidRuntime(1173):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:704)
E/AndroidRuntime(1173):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:345)
E/AndroidRuntime(1173):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
E/AndroidRuntime(1173):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
E/AndroidRuntime(1173):     at android.view.Window$LocalWindowManager.addView(Window.java:552)
E/AndroidRuntime(1173):     at android.widget.MediaController.show(MediaController.java:338)
E/AndroidRuntime(1173):     at android.webkit.HTML5VideoFullScreen.showControllerInFullScreen(HTML5VideoFullScreen.java:387)
E/AndroidRuntime(1173):     at android.webkit.HTML5VideoViewProxy$VideoPlayer.end(HTML5VideoViewProxy.java:310)
E/AndroidRuntime(1173):     at android.webkit.HTML5VideoViewProxy.handleMessage(HTML5VideoViewProxy.java:450)
E/AndroidRuntime(1173):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(1173):     at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(1173):     at android.app.ActivityThread.main(ActivityThread.java:4961)
E/AndroidRuntime(1173):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1173):     at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(1173):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
E/AndroidRuntime(1173):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
E/AndroidRuntime(1173):     at dalvik.system.NativeStart.main(Native Method)
D/skia(1173): --------- SkTypeface::CreateFromBuffer failed 19160

0 个答案:

没有答案