WebView视频全屏

集成了腾讯浏览服务X5内核了,发现手机没有装微信\QQ这些,且没有wifi的时候,无法自动加载内核。所以腾讯浏览服务SDK就会切换到系统内核(无需自己处理)。系统的webview就有很多问题了,例如视频全屏、最小化需要自己实现。

这是腾讯浏览服务SDK的方法:

 mWebView.setWebChromeClient(new WebChromeClient(){
         /**
         * 视频全屏调用此函数
         * @param view
         * @param customViewCallback
         */
        @Override
        public void onShowCustomView(View view, int i, IX5WebChromeClient.CustomViewCallback customViewCallback) {
            super.onShowCustomView(view, i, customViewCallback);
            setFullScreen(view,customViewCallback);
          }
        /**
         * 视频全屏调用此函数
         * @param view
         * @param customViewCallback
         */
        @Override
        public void onShowCustomView(View view, IX5WebChromeClient.CustomViewCallback customViewCallback) {
            super.onShowCustomView(view, customViewCallback);
            setFullScreen(view,customViewCallback);
        }
        /**
          *退出全屏调用此函数
         **/
          @Override
        public void onHideCustomView() {
              super.onHideCustomView();
         }  
  });

这是webview自己的方法:

   mWebView.setWebChromeClient(new WebChromeClient(){
 /**
     * 点击全屏
     * @param view
     * @param callback
     */
    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        super.onShowCustomView(view, callback);
    }
    /**
     * 点击全屏
     * @param view
     * @param requestedOrientation
     * @param callback
     */
    @Override
    public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
        super.onShowCustomView(view, requestedOrientation, callback);
    }

    /**
     * 取消全屏
     */
    @Override
    public void onHideCustomView() {
        super.onHideCustomView();
    }
});

两种的方法没有什么差异,有X5内核就会使用X5的,没有就使用系统的。
实现视频全屏的方式都是一样

内容简单,直接贴代码
WebViewActivity.java
退出全屏onHideCustomView()的时候,videoLayout一定不要removeView(),会导致无法在WebView弹虚拟键盘,原因未知。
只能在全屏的时候判断videoLayout是否存在有子view,有就清空。

    import android.app.Activity;
    import android.content.pm.ActivityInfo;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.WindowManager;
    import android.webkit.WebChromeClient;
    import android.webkit.WebResourceRequest;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.FrameLayout;

public class WebViewActivity extends AppCompatActivity {
WebView mWebView;

private View nVideoView = null;
private WebChromeClient.CustomViewCallback callback;
private FrameLayout videoLayout;//视频全屏

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

private void initView() {
    videoLayout = findViewById(R.id.video_layout);
    mWebView=findViewById(R.id.webview);
    WebSettings settings = mWebView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings.setPluginState(WebSettings.PluginState.ON);
    //settings.setPluginsEnabled(true);
    settings.setAllowFileAccess(true);
    settings.setLoadWithOverviewMode(true);
    settings.setUseWideViewPort(true);
    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    mWebView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(view.getUrl());
            return super.shouldOverrideUrlLoading(view, request);
        }
    });
    mWebView.setWebChromeClient(new CustomWebViewChromeClient());

    mWebView.loadUrl("http://www.pearvideo.com/video_1384268");

}

class CustomWebViewChromeClient extends WebChromeClient{

    /**
     * 点击全屏
     * @param view
     * @param callback
     */
    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        super.onShowCustomView(view, callback);
        setFullScreen(view,callback);
    }
    /**
     * 点击全屏
     * @param view
     * @param requestedOrientation
     * @param callback
     */
    @Override
    public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
        super.onShowCustomView(view, requestedOrientation, callback);
        setFullScreen(view,callback);
    }

    /**
     * 取消全屏
     */
    @Override
    public void onHideCustomView() {
        super.onHideCustomView();
        if (nVideoView == null) {
            return;
        }
        try {
            callback.onCustomViewHidden();
        } catch (Exception e) {
        }
        nVideoView.setVisibility(View.GONE);
        //如果在这里remove子view,会导致WebView无法弹出虚拟键盘。removeAllViews和removeView()都试过了,找了半天。原因未知
//            videoLayout.removeAllViews();
  //            videoLayout.removeView(nVideoView);
        nVideoView = null;
        videoLayout.setVisibility(View.GONE);
        // 设置竖屏
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        // 取消全屏
        final WindowManager.LayoutParams attrs =getWindow().getAttributes();
        attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setAttributes(attrs);
       getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }
}

/**
 * 设置全屏
 * @param view
 * @param customViewCallback
 */
private void setFullScreen(View view, WebChromeClient.CustomViewCallback customViewCallback){
    if (nVideoView != null) {
        customViewCallback.onCustomViewHidden();
        return;
    }
    nVideoView = view;
    nVideoView.setVisibility(View.VISIBLE);
    callback = customViewCallback;
    //退出全屏的时候无法remove,会导致WebView无法弹虚拟键盘,只能在设置全屏的时候清空之前add的
    if(videoLayout.getChildCount()>0){
        videoLayout.removeAllViews();
    }
    videoLayout.addView(nVideoView);
    videoLayout.setVisibility(View.VISIBLE);
    videoLayout.bringToFront();
    //设置横屏
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    //设置全屏
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}

布局 :activity_web_view.xml







  


清单文件:AndroidManifest.xml
需要设置切换横竖屏不会重新加载Activity
android:configChanges="orientation|keyboardHidden|screenSize"

 

你可能感兴趣的:(WebView视频全屏)