全屏时HTML 5视频崩溃

时间:2012-10-31 11:20:06

标签: android webview android-webview html5-video webchromeclient

我目前正在开发一个带屏幕方向肖像的应用程序,我有html 5视频需要全屏支持。我在webview中添加了一个WebChromeClient,它覆盖了一些方法。当我坚持纵向方向时,一切都工作得很好,但当我试图在全屏时将方向切换到风景时,我遇到了崩溃。任何线索?

覆盖方法:

public void onShowCustomView(View view, CustomViewCallback callback) {
    super.onShowCustomView(view, callback);
    if (mCustomViewContainer != null) {
        callback.onCustomViewHidden();
        return;
    }
    if(interfazWeb==null)
        interfazWeb = (FragTabActivity) getActivity();
        if (view instanceof FrameLayout) {
            mCustomViewContainer = (FrameLayout) view;
            mCustomViewCallback = callback;
            interfazWeb.getCustomContentView().setVisibility(View.INVISIBLE);
            interfazWeb.addContentView(mCustomViewContainer, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, Gravity.CENTER));
            interfazWeb.setBackDelegate(new BackDelegate(){

                public boolean shouldOverrideBackButton() {
                    if(mCustomViewCallback!=null){//first calling onHideCustomView()
                        onHideCustomView();
                        return true;
                    }
                    return false;
                }

            });
            mCustomViewContainer.bringToFront();
            //interfazWeb.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }

    @Override
    public void onHideCustomView() {
        // TODO Auto-generated method stub
            if(interfazWeb==null)
                interfazWeb = (FragTabActivity) getActivity();
            if(mCustomViewContainer!=null){
                ViewGroup parent = (ViewGroup) interfazWeb.getCustomContentView().getParent();
                if(parent!=null){
                    parent.removeView(mCustomViewContainer);
                    interfazWeb.getCustomContentView().setVisibility(View.VISIBLE);
                }
            }
            if(mCustomViewCallback!=null){
                mCustomViewCallback.onCustomViewHidden();
                mCustomViewCallback = null;
            }
            //interfazWeb.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        @Override
        public Bitmap getDefaultVideoPoster() {
            // TODO Auto-generated method stub
            return Bitmap.createBitmap(1, 1, Config.ARGB_8888);
        }

        @Override
        public View getVideoLoadingProgressView() {
            // TODO Auto-generated method stub
            return new ProgressBar((Context) getActivity());
        }

日志说:

10-31 11:09:36.336: E/AndroidRuntime(8098): java.lang.NullPointerException
10-31 11:09:36.336: E/AndroidRuntime(8098):     at android.webkit.HTML5VideoView.isPlaying(HTML5VideoView.java:122)
10-31 11:09:36.336: E/AndroidRuntime(8098):     at android.webkit.HTML5VideoViewProxy$VideoPlayer.isPlaying(HTML5VideoViewProxy.java:253)
10-31 11:09:36.336: E/AndroidRuntime(8098):     at android.webkit.HTML5VideoViewProxy.handleMessage(HTML5VideoViewProxy.java:402)
10-31 11:09:36.336: E/AndroidRuntime(8098):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 11:09:36.336: E/AndroidRuntime(8098):     at android.os.Looper.loop(Looper.java:137)
10-31 11:09:36.336: E/AndroidRuntime(8098):     at android.app.ActivityThread.main(ActivityThread.java:4758)
10-31 11:09:36.336: E/AndroidRuntime(8098):     at java.lang.reflect.Method.invokeNative(Native Method)
10-31 11:09:36.336: E/AndroidRuntime(8098):     at java.lang.reflect.Method.invoke(Method.java:511)
10-31 11:09:36.336: E/AndroidRuntime(8098):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-31 11:09:36.336: E/AndroidRuntime(8098):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-31 11:09:36.336: E/AndroidRuntime(8098):     at dalvik.system.NativeStart.main(Native Method)

编辑:我的清单中有这一行:

android:configChanges="keyboardHidden|orientation|locale|screenLayout"

2 个答案:

答案 0 :(得分:0)

当您切换方向时,应用程序将自行重置为oncreate事件。它不仅仅是简单地改变高度和宽度以及相关的东西。这就是为什么你得到一个null异常的原因,这次你创建的控件之一就不存在了。可能是HTML5View。

您应该做的是跟踪视频位置,如果开关重新加载所有控件,请从最后一个位置开始。

答案 1 :(得分:0)

您可以尝试通过将此添加到您的活动来处理Manifest中的方向更改:

android:configChanges="orientation|screenLayout"

我曾经有同样的问题,但这对我有用。