如何在全屏模式下播放视频并在Android WebView中退出全屏模式?

时间:2017-07-21 09:36:29

标签: android video webview fullscreen

我知道这是一个古老的问题,并且多次被问过。但我尝试了很多解决方案,他们不能和我合作。

我在华为6P和三星S7,Nexus 5中进行了测试, WebView中的视频(https://www.youtube-nocookie.com/embed/M68lNfqmTNk?rel=0&rel=0&showinfo=0)无法按预期工作(去年有效)。 当我单击全屏按钮时,会连续调用“onShowCustomView”和“onHideCustomView”。所以我可以看到视频闪烁(非全屏 - >全屏 - >非全屏)。 有时如果我很幸运,视频会进入全屏模式,但如果按全屏按钮退出全屏模式,该按钮根本不起作用。

我在GitHub写了一个演示,希望有人可以帮助我。感谢。

这是代码

Init Webview:

    mWebView = (WebView)findViewById(R.id.webview);

    mWebChromeClient = new MyWebChromeClient();
    mWebView.setWebChromeClient(mWebChromeClient);
    mWebView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            mWebView.loadUrl(url);
            return true;
        }
    });
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setUseWideViewPort(true);
    webSettings.setAllowFileAccess(true);
    webSettings.setSupportZoom(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

MyWebChromeClient:

 public class MyWebChromeClient extends WebChromeClient {

    FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        Log.d("youtube", "onShowCustomView");
        // if a view already exists then immediately terminate the new one
        if (mCustomView != null) {
            callback.onCustomViewHidden();
            return;
        }
        mContentView = (ConstraintLayout) findViewById(R.id.activity_main);
        mContentView.setVisibility(View.GONE);
        mCustomViewContainer = new FrameLayout(MainActivity.this);
        mCustomViewContainer.setLayoutParams(LayoutParameters);
        mCustomViewContainer.setBackgroundResource(android.R.color.black);
        view.setLayoutParams(LayoutParameters);
        mCustomViewContainer.addView(view);
        mCustomView = view;
        mCustomViewCallback = callback;
        mCustomViewContainer.setVisibility(View.VISIBLE);
        setContentView(mCustomViewContainer);
    }

    @Override
    public void onHideCustomView() {
        Log.d("youtube", "onHideCustomView");
        if (mCustomView == null) {
            return;
        } else {
            // Hide the custom view.
            mCustomView.setVisibility(View.GONE);
            // Remove the custom view from its container.
            mCustomViewContainer.removeView(mCustomView);
            mCustomView = null;
            mCustomViewContainer.setVisibility(View.GONE);
            mCustomViewCallback.onCustomViewHidden();
            // Show the content view.
            mContentView.setVisibility(View.VISIBLE);
            setContentView(mContentView);
        }
    }

}

我在AndroidManifest.xml中设置了android:hardwareAccelerated

android:hardwareAccelerated="true"

3 个答案:

答案 0 :(得分:1)

要在网页浏览中加载YouTube视频,您必须改用iframe, 例如:

<iframe width="300" height=".." src="http://www.youtube.com/embed/M68lNfqmTNk" frameborder="0" allowfullscreen></iframe>

并将html文本加载到webview,如下所示:<html><body> <iframe width="330" height="315" src="http://www.youtube.com/embed/M68lNfqmTNk" frameborder="0" allowfullscreen></iframe>....

答案 1 :(得分:0)

我认为已经为这些问题预定了许多答案 但我想你需要在markparnelllink

之后检查一下这个答案

在该链接中标记非常详细的解决方案

答案 2 :(得分:0)

在您的网页浏览中添加此代码。 webView.getSettings().setUserAgentString("Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");