webview全屏不显示全屏视频

Posted

技术标签:

【中文标题】webview全屏不显示全屏视频【英文标题】:webview full screen not show full screen video 【发布时间】:2019-09-06 07:19:19 【问题描述】:

有些人可能会将其标记为重复问题。在将其标记为重复之前,请完整阅读我的问题。最近两天我遇到了这个问题。我已经尝试了从 *** 获得的所有答案,甚至从 github 克隆了一些项目。但是没有答案对我有用,最重要的是几天前同样的代码对我有用。我得到的所有答案都是很久以前的。如果可以的话请帮助我..在我的代码下面

webView.setWebViewClient(new AppWebViewClients());
                webView.setWebChromeClient(new MyWebClient());
                WebSettings webSettings = webView.getSettings();
                webSettings.setjavascriptEnabled(true);
                webSettings.setDomStorageEnabled(true);


                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) 
                    webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
                
                try 
                    webView.loadUrl("https://www.youtube.com/watch?v=hlvbDjksdCg");
                 catch (Exception e) 
                    Log.d(TAG, "run: " + e.getMessage());
                

我的网络客户端

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

    MyWebClient() 
    

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

    public void onHideCustomView() 
        ((FrameLayout) StreamActivity.this.getWindow().getDecorView()).removeView(this.mCustomView);
        this.mCustomView = null;
        StreamActivity.this.getWindow().getDecorView().setSystemUiVisibility(0);
        StreamActivity.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 = StreamActivity.this.getWindow().getDecorView().getSystemUiVisibility();
        this.mOriginalOrientation = StreamActivity.this.getRequestedOrientation();
        this.mCustomViewCallback = paramCustomViewCallback;
        ((FrameLayout) StreamActivity.this.getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
        StreamActivity.this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);


    

最后是 webview 客户端

公共类 AppWebViewClients 扩展 WebViewClient

AppWebViewClients() 
    pd=Util.createProgressDialog(StreamActivity.this);
    pd.show();

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) 

    view.loadUrl(url);
    return true;


@Override
public void onPageFinished(WebView view, String url) 
    super.onPageFinished(view, url);
    pd.dismiss();




【问题讨论】:

它可能取决于您的网络代码中的视频播放器。您是否尝试过在 Chrome 上播放它并进入全屏模式? 我已经尝试使用服务器提供的 url 它在 chrome 中工作 当您尝试在 webview 上全屏显示时会发生什么?您是否尝试过使用不同的网络引擎? 点击全屏时的日志 btn: I/chromium: [INFO:CONSOLE(0)] "无法在 'Element' 上执行 'requestFullscreen':API 只能由用户手势启动。 ",来源:test...我正在尝试使用 chrome 我在某处读到 chromium 与 Fullscreen API 存在一些问题,如果可以访问,您可能需要对 javascript 进行一些修改。阅读此***.com/questions/18966923/… 【参考方案1】:

试试这个

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

    @Override
    public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) 
        if (this.mCustomView != null) 
            onHideCustomView();
            return;
        
        this.mCustomView = paramView;
        this.mOriginalSystemUiVisibility = StreamActivity.this.getWindow().getDecorView().getSystemUiVisibility();
        this.mOriginalOrientation = StreamActivity.this.getRequestedOrientation();
        this.mCustomViewCallback = paramCustomViewCallback;
        ((FrameLayout) StreamActivity.this.getWindow().getDecorView()).addView(this.mCustomView, 
                new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));
        int visibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) 
            visibility = visibility | View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_FULLSCREEN;
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) 
            visibility = visibility | View.SYSTEM_UI_FLAG_IMMERSIVE;
        
        StreamActivity.this.getWindow().getDecorView().setSystemUiVisibility(visibility);
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    

并且必须在 androidManifest.xml 中设置活动 configChanges 属性

<activity
        android:name=".StreamActivity"
        ...
        android:configChanges="keyboardHidden|orientation|screenSize" />

【讨论】:

不工作.. 点击全屏 btn 时记录日志:I/chromium: [INFO:CONSOLE(0)] "无法在 'Element' 上执行 'requestFullscreen':API 只能由用户手势。”,来源:test

以上是关于webview全屏不显示全屏视频的主要内容,如果未能解决你的问题,请参考以下文章

“全屏不可用”将YouTube视频作为iframe嵌入QWebEngineView中

在所有元素顶部显示全屏不确定进度条[重复]

pyqt5窗口全屏不显示边框

使 AVPlayer 全屏不加信箱

webview 中的视频应在 android 中的 onShowCustomView() 方法上全屏显示

Android Webview 应用程序不会让视频播放器全屏显示