当显示全屏视频播放器控件(可见)时,状态栏返回(可见)

时间:2016-08-21 13:04:37

标签: android android-webview android-video-player

我使用WebView加载视频。我覆盖了onShowCustomView(进入全屏)和onHideCustomView(以完全脱离屏幕)方法。 当我进入全屏模式时,变量fullscreen设置为trueOnSystemUiVisibilityChangeListener调用GoFullscreen()函数设置SYSTEM_UI_FLAG_FULLSCREEN标志并隐藏操作栏:

decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN); getSupportActionBar().hide();

但是当我进入全屏模式时,如果视频控件可见,则状态栏可见。因此,仅当视频控件消失(不可见)时,状态栏才会隐藏。

屏幕截图,查看视频和状态栏控件的问题:

------------ 正常(确定)

normal mode

----- 全屏带视频控件(不行)-------- 全屏无视频控件(确定) enter image description here enter image description here

代码:

public class TestActivity extends AppCompatActivity {
private MyWebChromeClient mWebChromeClient = null;
private View mCustomView;
private LinearLayout mContentView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private Bundle webViewBundle;
private WebView mWebView;
private int viewWidth;
private int viewHeight;
private LinearLayout.LayoutParams layoutParams;
private View decorView;
private Boolean Fullscreen = false;

private void GoFullscreen() {
    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
    getSupportActionBar().hide();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    decorView = getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
        @Override
        public void onSystemUiVisibilityChange(int visibility) {
            if (Fullscreen) {
                GoFullscreen();
            } else {
                decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                getSupportActionBar().show();
            }
        }
    });

    mWebView = (WebView) findViewById(R.id.webView);
    mWebView.setBackgroundColor(Color.BLACK);
    mWebChromeClient = new MyWebChromeClient();
    mWebView.setWebChromeClient(mWebChromeClient);
    mWebView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    viewWidth = 480;
    viewHeight = (int) (((double) viewWidth) * 0.5625); // video aspect 16:9
    layoutParams = new LinearLayout.LayoutParams(viewWidth, viewHeight);
    mWebView.setLayoutParams(layoutParams);

    if (webViewBundle != null) {
        mWebView.restoreState(webViewBundle);
    } else {
        mWebView.loadUrl("http://vk.com/video_ext.php?oid=-89326201&id=171643607&hash=2b18b090e13222dc&hd=1");
    }
}

@Override
public void onPause() {
    super.onPause();
    webViewBundle = new Bundle();
    mWebView.saveState(webViewBundle);
    mWebView.onPause();
}

@Override
public void onResume() {
    super.onResume();
    mWebView.onResume();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mWebView.destroy();
}

public class MyWebChromeClient extends WebChromeClient {
    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        // if a view already exists then immediately terminate the new one
        if (mCustomView != null) {
            callback.onCustomViewHidden();
            return;
        }
        Fullscreen = true;
        FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        mContentView = (LinearLayout) findViewById(R.id.activity_main);
        mCustomViewContainer = new FrameLayout(TestActivity.this);
        mCustomViewContainer.setLayoutParams(LayoutParameters);
        mCustomViewContainer.setBackgroundResource(android.R.color.black);
        view.setLayoutParams(LayoutParameters);
        mCustomViewContainer.addView(view);
        mCustomView = view;
        mCustomViewCallback = callback;
        mContentView.setVisibility(View.GONE);
        GoFullscreen();
        mCustomViewContainer.setVisibility(View.VISIBLE);
        setContentView(mCustomViewContainer);
    }

    @Override
    public void onHideCustomView() {
        if (mCustomView != null) {
            // Hide the custom view.
            mCustomView.setVisibility(View.GONE);
            // Remove the custom view from its container.
            mCustomViewContainer.removeView(mCustomView);
            mCustomView = null;
            mCustomViewContainer.setVisibility(View.GONE);
            mCustomViewContainer = null;
            mCustomViewCallback.onCustomViewHidden();
            // Show the content view.
            Fullscreen = false;
            mContentView.setVisibility(View.VISIBLE);
            setContentView(mContentView);
        }
    }
}

@Override
public void onBackPressed() {
    if (mCustomViewContainer != null)
        mWebChromeClient.onHideCustomView();
    else if (mWebView.canGoBack())
        mWebView.goBack();
    else
        super.onBackPressed();
}

}

如何解决?

P.S。 build.gradle API:compileSdkVersion 23 / buildToolsVersion“23.0.1”/ minSdkVersion 19 / targetSdkVersion 23

UPDATE:似乎仅在KitKat上出现问题。我已经在API 23智能手机上进行了测试,它没有这个问题。仍然需要为KitKat设备修复它

2 个答案:

答案 0 :(得分:0)

您的所有内容仍然相同,只需更改FullScreen方法,如下所示

public class MainActivity extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


        }

        private void GoFullscreen() {
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE);
        }

    }

答案 1 :(得分:0)

最终我决定不应该使用OnSystemUiVisibilityChangeListener(它会工作有问题),只需在FullscreenOnonShowCustomViewFullscreenOff拨打onHideCustomView。这样做会很好,但仍然无法完全隐藏KitKat的侧边栏(但对其他版本的Android有效)

    private void FullscreenOn() {
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        getSupportActionBar().hide();
    }

    private void FullscreenOff() {
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        getSupportActionBar().show();
    }