在WebView中加载YouTube视频时,无法使用全屏选项

时间:2016-03-29 04:13:21

标签: javascript android webview youtube android-webview

我在Android应用程序中使用webview工作了这么多时间。但是这次把youtube视频加载到WebView中我遇到了奇怪的问题。

请参阅,这是在Chrome浏览器中加载的YouTube视频的屏幕截图,其中包含全屏选项。

enter image description here

现在,下面是我的应用程序的屏幕截图,我在webview中加载了相同的视频。但它没有全屏选项。

enter image description here

您可以看到两张图片中的更改。两个屏幕截图都来自同一设备。但它看起来仍然不同。

我的webView加载代码是HERE IN THIS LINK.

我希望有可能做到这一点。需要一些帮助。 感谢。

更新

我也看到同样的问题被报道HERE.但是不知道是否有可用的解决方案。

此致 Shreyash

5 个答案:

答案 0 :(得分:26)

在您的java文件中执行以下更改:

view.setWebViewClient(new Browser());
view.setWebChromeClient(new MyWebClient());

并在java文件中添加类2 Browser和类MyWebClient的这个类

class Browser
        extends WebViewClient
{
    Browser() {}

    public boolean shouldOverrideUrlLoading(WebView paramWebView, String paramString)
    {
        paramWebView.loadUrl(paramString);
        return true;
    }
}

public class MyWebClient
        extends WebChromeClient
{
    private View mCustomView;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    protected FrameLayout mFullscreenContainer;
    private int mOriginalOrientation;
    private int mOriginalSystemUiVisibility;

    public MyWebClient() {}

    public Bitmap getDefaultVideoPoster()
    {
        if (MainActivity.this == null) {
            return null;
        }
        return BitmapFactory.decodeResource(MainActivity.this.getApplicationContext().getResources(), 2130837573);
    }

    public void onHideCustomView()
    {
        ((FrameLayout)MainActivity.this.getWindow().getDecorView()).removeView(this.mCustomView);
        this.mCustomView = null;
        MainActivity.this.getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
        MainActivity.this.setRequestedOrientation(this.mOriginalOrientation);
        this.mCustomViewCallback.onCustomViewHidden();
        this.mCustomViewCallback = null;
    }

    public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
    {
        if (this.mCustomView != null)
        {
            onHideCustomView();
            return;
        }
        this.mCustomView = paramView;
        this.mOriginalSystemUiVisibility = MainActivity.this.getWindow().getDecorView().getSystemUiVisibility();
        this.mOriginalOrientation = MainActivity.this.getRequestedOrientation();
        this.mCustomViewCallback = paramCustomViewCallback;
        ((FrameLayout)MainActivity.this.getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
        MainActivity.this.getWindow().getDecorView().setSystemUiVisibility(3846);
    }
}

答案 1 :(得分:8)

<textarea disabled="disabled" style="color:#000 ;">Hello</textarea> 是一个选项,但你可以试试这个

  

Android启用的WebView和WebChromeClient类扩展   完全正常的HTML5视频支持

VideoEnabledWebView

我还没试过,但希望对你有所帮助。

答案 2 :(得分:2)

如果我理解正确,你的iframe包含第二个iframe(youtube one)。 尝试将allowfullscreen属性添加到“父”iframe。

要获得完整的浏览器支持,它应如下所示:

<iframe src="your_page_url" allowfullscreen="allowfullscreen" mozallowfullscreen="mozallowfullscreen" msallowfullscreen="msallowfullscreen" oallowfullscreen="oallowfullscreen" webkitallowfullscreen="webkitallowfullscreen"> </iframe>

答案 3 :(得分:0)

//Add WebChromeClient to your webview 
//With navigation option and player controls overlapping handlled.

    class UriChromeClient extends WebChromeClient {
            private View mCustomView;
            private WebChromeClient.CustomViewCallback mCustomViewCallback;
            protected FrameLayout mFullscreenContainer;
            private int mOriginalOrientation;
            private int mOriginalSystemUiVisibility;

            @SuppressLint("SetJavaScriptEnabled")
            @Override
            public boolean onCreateWindow(WebView view, boolean isDialog,
                                          boolean isUserGesture, Message resultMsg) {
                mWebviewPop = new WebView(getApplicationContext());
                mWebviewPop.setVerticalScrollBarEnabled(false);
                mWebviewPop.setHorizontalScrollBarEnabled(false);
                mWebviewPop.setWebViewClient(new MyWebViewClient());
                mWebviewPop.getSettings().setSupportMultipleWindows(true);
                mWebviewPop.getSettings().setJavaScriptEnabled(true);
                mWebviewPop.getSettings().setUserAgentString(mWebviewPop.getSettings().getUserAgentString().replace("; wv", ""));
    //            mWebviewPop.getSettings().setUserAgentString(USER_AGENT);
                mWebviewPop.getSettings().setSaveFormData(true);
                mWebviewPop.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));
                mContainer.addView(mWebviewPop);
                WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
                transport.setWebView(mWebviewPop);
                resultMsg.sendToTarget();

                return true;
            }

            @Override
            public void onCloseWindow(WebView window) {
                Log.d("onCloseWindow", "called");
            }

            //
            public Bitmap getDefaultVideoPoster() {
                if (mCustomView == null) {
                    return null;
                }
                return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
            }

            public void onHideCustomView() {
                ((FrameLayout) getWindow().getDecorView()).removeView(this.mCustomView);
                this.mCustomView = null;
                getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
                setRequestedOrientation(this.mOriginalOrientation);
                this.mCustomViewCallback.onCustomViewHidden();
                this.mCustomViewCallback = null;
            }

            public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) {
                if (this.mCustomView != null) {
                    onHideCustomView();
                    return;
                }
                this.mCustomView = paramView;
                this.mCustomView.setBackgroundColor(Color.BLACK);
                this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
                this.mOriginalOrientation = getRequestedOrientation();
                this.mCustomViewCallback = paramCustomViewCallback;
                ((FrameLayout) getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
                getWindow().getDecorView().setSystemUiVisibility(3846);

                this.mCustomView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
                            updateControls(getNavigationBarHeight());
                        } else {
                            updateControls(0);
                        }

                    }
                });

            }

            void updateControls(int bottomMargin) {
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) this.mCustomView.getLayoutParams();
                params.bottomMargin = bottomMargin;
                this.mCustomView.setLayoutParams(params);
            }
        }

        int getNavigationBarHeight() {
            Resources resources = getResources();
            int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                return resources.getDimensionPixelSize(resourceId);
            }
            return 0;
        }


        private void loadURL(WebView view, String url) {
            ConnectivityManager cm = (ConnectivityManager) getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnected()) {
                view.setVisibility(View.VISIBLE);
                noNetworkText.setVisibility(View.GONE);
                view.loadUrl(url);

            } else {
                Log.d(TAG, "loadURL: no network");
                view.setVisibility(View.INVISIBLE);
                noNetworkText.setVisibility(View.VISIBLE);
            }
        }

    }

答案 4 :(得分:0)

@Ameya Bonkinpelliwar在科特林的答案

class WebChrome(activity: Activity) : WebChromeClient() {

    private val activityRef = WeakReference(activity)

    private var customView: View? = null
    private var customViewCallback: CustomViewCallback? = null

    private var originalOrientation = 0
    private var originalSystemUiVisibility = 0

    override fun onProgressChanged(view: WebView, progress: Int) {
        view.context.activityCallback<MainActivity> {
            onProgress(progress)
        }
    }

    override fun getDefaultVideoPoster(): Bitmap? {
        return activityRef.get()?.run {
            BitmapFactory.decodeResource(applicationContext.resources, 2130837573)
        }
    }

    override fun onHideCustomView() {
        activityRef.get()?.run {
            (window.decorView as ViewGroup).removeView(customView)
            customView = null
            window.decorView.systemUiVisibility = originalSystemUiVisibility
            requestedOrientation = originalOrientation
        }
        customViewCallback?.onCustomViewHidden()
        customViewCallback = null
    }

    override fun onShowCustomView(view: View?, viewCallback: CustomViewCallback?) {
        if (customView != null) {
            onHideCustomView()
            return
        }
        customView = view
        activityRef.get()?.run {
            originalSystemUiVisibility = window.decorView.systemUiVisibility
            originalOrientation = requestedOrientation
            customViewCallback = viewCallback
            (window.decorView as ViewGroup).addView(
                customView,
                ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            )
            window.decorView.systemUiVisibility = 3846
        }
    }
}