webView全屏播放视频类

三步走:

 

1、Androidmanifest.xml给你播放视频的webview所在activity添加如下代码:

hardwareAccelerated:开启加速

configChanges:界面发生横竖屏切换的配置

 

2、Layout布局:




    
    

    

    
    

    

    
    

 

3、activity类:

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

public class WebVideoPlayer extends AppCompatActivity {

    private WebView mWebView;
    private ProgressBar progressBar;
    private FrameLayout fullVideo;
    protected View customView;

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

        LinearLayout linearLayout = findViewById(R.id.ll_web);
        progressBar = findViewById(R.id.progressBar);

        fullVideo = findViewById(R.id.fl_video_full);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mWebView = new WebView(getApplicationContext());
        mWebView.setLayoutParams(params);
        linearLayout.addView(mWebView);

        initWebView(getIntent().getStringExtra("url"));
    }

    private void initWebView(String url) {
        WebSettings mWebSettings = mWebView.getSettings();
        mWebSettings.setSupportZoom(true);
        mWebSettings.setLoadWithOverviewMode(true);
        mWebSettings.setUseWideViewPort(true);
        mWebSettings.setDefaultTextEncodingName("utf-8");
        mWebSettings.setLoadsImagesAutomatically(true);
        mWebSettings.setDomStorageEnabled(true);


        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.setWebChromeClient(new MyWebChromeClient());
        mWebView.loadUrl(url);
    }

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

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

    @Override
    protected void onDestroy() {
        if (mWebView != null) {
            mWebView.clearHistory();
            ((ViewGroup) mWebView.getParent()).removeView(mWebView);
            mWebView.loadUrl("about:blank");
            mWebView.stopLoading();
            mWebView.setWebChromeClient(null);
            mWebView.setWebViewClient(null);
            mWebView.destroy();
            mWebView = null;
        }
        super.onDestroy();
    }

    private class MyWebChromeClient extends WebChromeClient {

        @Override
        public void onProgressChanged(WebView view, int position) {
            progressBar.setProgress(position);
            if (position == 100) {
                progressBar.setVisibility(View.GONE);
            }
            super.onProgressChanged(view, position);
        }

        @Override
        public void onHideCustomView() {
            //退出全屏
            if (customView == null){
                return;
            }
            //如果你的界面有actionbar的话:
            //getSupportActionBar().show();
            //移除全屏视图并隐藏
            fullVideo.removeView(customView);
            fullVideo.setVisibility(View.GONE);
            //设置竖屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            //清除全屏
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        }

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            //如果你的界面有actionbar的话:
            //getSupportActionBar().hide();
            //进入全屏
            customView = view;
            fullVideo.setVisibility(View.VISIBLE);
            fullVideo.addView(customView);
            fullVideo.bringToFront();
            //设置横屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            //设置全屏
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }
}

可直接复制这个类和布局来用。

 

完!

你可能感兴趣的:(Android)