视频无法以全屏模式播放

时间:2012-02-02 07:23:26

标签: android webview android-4.0-ice-cream-sandwich

在FULLSCREEN尝试在webview中播放嵌入视频时,我正在获取NPE。它适用于3.0蜂窝,但不适用于ICS 4.0+。知道如何解决这个问题吗?

java.lang.NullPointerException
    at android.webkit.PluginFullScreenHolder.show(PluginFullScreenHolder.java:85)
    at android.webkit.WebView$PrivateHandler.handleMessage(WebView.java:8849)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)

任何建议都会非常感激。感谢

2 个答案:

答案 0 :(得分:1)

尝试将WebChromeClient添加到WebView:

webView.setWebChromeClient(new WebChromeClient());

这应该可以阻止崩溃(我认为它与我们遇到的情况类似),但我认为它不会让视频全屏播放。

要做到这一点,我认为你必须做这样的事情:Android ICS 4.0 Placing Flash WebView into full screen calls hideAll Method?我仍然想弄清楚。

答案 1 :(得分:1)

问题是Android操作系统中的一个错误。你需要一个解决这个问题的方法。
我遇到了同样的问题。以下工作为我工作。
希望这会帮助一些人:
创建FullscreenableChromeClient和 添加以下行:

WebView.setWebChromeClient( new FullscreenableChromeClient(this));


    public class FullscreenableChromeClient extends WebChromeClient {
        protected Activity mActivity = null;

        private View mCustomView;
        private WebChromeClient.CustomViewCallback mCustomViewCallback;
        private int mOriginalOrientation;

        private FrameLayout mContentView;
        private FrameLayout mFullscreenContainer;

        private static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        public FullscreenableChromeClient(Activity activity) {
            this.mActivity = activity;
        }

        @Override
        public void onShowCustomView(View view, int requestedOrientation, WebChromeClient.CustomViewCallback callback) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                if (mCustomView != null) {
                    callback.onCustomViewHidden();
                    return;
                }

                mOriginalOrientation = mActivity.getRequestedOrientation();
                FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
                mFullscreenContainer = new FullscreenHolder(mActivity);
                mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
                decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
                mCustomView = view;
                setFullscreen(true);
                mCustomViewCallback = callback;
                mActivity.setRequestedOrientation(requestedOrientation);
            }

            super.onShowCustomView(view, requestedOrientation, callback);
        }

        @Override
        public void onHideCustomView() {
            if (mCustomView == null) {
                return;
            }

            setFullscreen(false);
            FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
            decor.removeView(mFullscreenContainer);
            mFullscreenContainer = null;
            mCustomView = null;
            mCustomViewCallback.onCustomViewHidden();
            mActivity.setRequestedOrientation(mOriginalOrientation);
        }

        private void setFullscreen(boolean enabled) {
            Window win = mActivity.getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
            if (enabled) {
                winParams.flags |= bits;
            } else {
                winParams.flags &= ~bits;
                if (mCustomView != null) {
                    mCustomView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                } else {
                    mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                }
            }
            win.setAttributes(winParams);
        }

        private static class FullscreenHolder extends FrameLayout {
            public FullscreenHolder(Context ctx) {
                super(ctx);
                setBackgroundColor(ctx.getResources().getColor(android.R.color.black));
            }

            @Override
            public boolean onTouchEvent(MotionEvent evt) {
            return true;
        }
    }
}