webview全屏不显示全屏视频

时间:2019-04-16 08:39:01

标签: java android android-webview webchromeclient

可能有人将其标记为重复问题。在将其标记为重复之前,请完整阅读我的问题。我过去两天都遇到了这个问题。我已经尝试了所有从stackoverflow获得的答案,甚至从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());
                }

MyWebClient

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();
}


}

1 个答案:

答案 0 :(得分:0)

尝试

@   VM31193:1784

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

@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);
    }