javascript - 在 WebView 中加载 YouTube 视频时全屏选项不可用

标签 javascript android webview youtube android-webview

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

看,这是在 Chrome 浏览器中加载的 YouTube 视频的屏幕截图,其中有全屏选项。

enter image description here

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

enter image description here

您可以在两张图片中看到变化。两个屏幕截图均来自同一设备。但它看起来还是不一样。

我的 webView 加载代码是 in this pasteboard .

更新

我也看到同样的问题是reported here .但是不知道有没有解决办法。

最佳答案

在您的 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);
    }
}

关于javascript - 在 WebView 中加载 YouTube 视频时全屏选项不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36275299/

相关文章:

javascript - MDN 中的 Symbol.species 示例没有意义?

javascript - 处理 JSON 对象数组

javascript - 我可以绕过可选参数并仍然在 Javascript 中设置剩余参数吗?

android - Volley 内容类型 header 未更新

android - 使用持久通知允许用户返回运行 Android 应用程序

android - 在 WebView 中加载大部分页面后关闭 ProgressCircle

java - Android Webview : Cannot call determinedVisibility() - never saw a connection for the pid

javascript - Meteor:如何对 session 变量的集合进行排序?

android - 如何添加多个权限?一个来自 list ,一个来自运行时

javascript - WebView 使用 javascript getElementsByClassName() 无法正确显示