在APP開發(fā)的過程中,會(huì)碰到需要在WebView中播放視頻的需求,下面講解一下如何在WebView中使用html5播放視頻。
1.讓視頻在各個(gè)Android版本能夠正常播放
在AndroidManifest.xml中聲明HardwareAccelerate的標(biāo)志,一般是添加在Activity的級(jí)別上。代碼如下:
<activity ... android:hardwareAccelerated="true">
下面引申一下HardwareAccelerate聲明的方式:
(a).如果需要聲明整個(gè)應(yīng)用都要加速,則在Application級(jí)別下面進(jìn)行聲明:
< application ... android:hardwareAccelerated ="true">
(b).如果需要某個(gè)Activity加速,則可以進(jìn)行下面的聲明:
<activity ... android:hardwareAccelerated="true">
或者在代碼里面進(jìn)行動(dòng)態(tài)的聲明:
getWindow.setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
在AndroidManifest.xml中聲明HardwareAccelerate的標(biāo)志,一般是添加在Activity的級(jí)別上。代碼如下:
<activity ... android:hardwareAccelerated="true">
下面引申一下HardwareAccelerate聲明的方式:
(a).如果需要聲明整個(gè)應(yīng)用都要加速,則在Application級(jí)別下面進(jìn)行聲明:
< application ... android:hardwareAccelerated ="true">
(b).如果需要某個(gè)Activity加速,則可以進(jìn)行下面的聲明:
<activity ... android:hardwareAccelerated="true">
或者在代碼里面進(jìn)行動(dòng)態(tài)的聲明:
getWindow.setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
2. 視頻播放的時(shí)候能夠全屏
public class WebVideoActivity extends Activity {
? ? private WebView webView;
? ? /** 視頻全屏參數(shù) */
? ? protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
? ? private View customView;
? ? private FrameLayout fullscreenContainer;
? ? private WebChromeClient.CustomViewCallback customViewCallback;
? ? @Override
? ? protected void onCreate(Bundle bundle) {
? ? ? ? super.onCreate(bundle);
? ? ? ? setContentView(R.layout.activity_xx);
? ? ? ? webView = (WebView) findViewById(R.id.xx);
? ? ? ? initWebView();
? ? }
? ? @Override
? ? protected void onStop() {
? ? ? ? super.onStop();
? ? ? ? webView.reload();
? ? }
? ? /** 展示網(wǎng)頁(yè)界面 **/ public void initWebView() {
? ? ? ? WebChromeClient wvcc = new WebChromeClient();
? ? ? ? WebSettings webSettings = webView.getSettings();
? ? ? ? webSettings.setJavaScriptEnabled(true);
? ? ? ? webSettings.setUseWideViewPort(true); // 關(guān)鍵點(diǎn)
? ? ? ? webSettings.setLoadWithOverviewMode(true);
? ? ? ? webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); // 不加載緩存內(nèi)容
? ? ? ? webView.setWebChromeClient(wvcc);
? ? ? ? WebViewClient wvc = new WebViewClient() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean shouldOverrideUrlLoading(WebView view, String url) {
? ? ? ? ? ? ? ? webView.loadUrl(url);
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? webView.setWebViewClient(wvc);
? ? ? ? webView.setWebChromeClient(new WebChromeClient() {
? ? ? ? ? ? /*** 視頻播放相關(guān)的方法 **/
? ? ? ? ? ? @Override
? ? ? ? ? ? public View getVideoLoadingProgressView() {
? ? ? ? ? ? ? ? FrameLayout frameLayout = new FrameLayout(WebVideoActivity.this);
? ? ? ? ? ? ? ? frameLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
? ? ? ? ? ? ? ? return frameLayout;
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onShowCustomView(View view, CustomViewCallback callback) {
? ? ? ? ? ? ? ? showCustomView(view, callback);
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onHideCustomView() {
? ? ? ? ? ? ? ? hideCustomView();
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? // 加載Web地址
? ? ? ? webView.loadUrl(webUrl);
? ? }
? ? /** 視頻播放全屏 **/
? ? private void showCustomView(View view, CustomViewCallback callback) {
? ? ? ? // if a view already exists then immediately terminate the new one
? ? ? ? if (customView != null) {
? ? ? ? ? ? callback.onCustomViewHidden();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? WebVideoActivity.this.getWindow().getDecorView();
? ? ? ? FrameLayout decor = (FrameLayout) getWindow().getDecorView();
? ? ? ? fullscreenContainer = new FullscreenHolder(WebVideoActivity.this);
? ? ? ? fullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
? ? ? ? decor.addView(fullscreenContainer, COVER_SCREEN_PARAMS);
? ? ? ? customView = view;
? ? ? ? setStatusBarVisibility(false);
? ? ? ? customViewCallback = callback;
fullScreen();
? ? }
? ? /** 隱藏視頻全屏 */
? ? private void hideCustomView() {
? ? ? ? if (customView == null) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? setStatusBarVisibility(true);
? ? ? ? FrameLayout decor = (FrameLayout) getWindow().getDecorView();
? ? ? ? decor.removeView(fullscreenContainer);
? ? ? ? fullscreenContainer = null;
? ? ? ? customView = null;
? ? ? ? customViewCallback.onCustomViewHidden();
? ? ? ? webView.setVisibility(View.VISIBLE);
fullScreen();
? ? }
? ? /** 全屏容器界面 */
? ? static class FullscreenHolder extends FrameLayout {
? ? ? ? public FullscreenHolder(Context ctx) {
? ? ? ? ? ? super(ctx);
? ? ? ? ? ? setBackgroundColor(ctx.getResources().getColor(android.R.color.black));
? ? ? ? }
? ? ? ? @Override
? ? ? ? public boolean onTouchEvent(MotionEvent evt) {
? ? ? ? ? ? return true;
? ? ? ? }
? ? }
? ? private void setStatusBarVisibility(boolean visible) {
? ? ? ? int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
? ? ? ? getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
? ? }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
? ? switch (keyCode) {
? ? ? ? case KeyEvent.KEYCODE_BACK:
? ? ? ? ? ? /** 回退鍵 事件處理 優(yōu)先級(jí):視頻播放全屏-網(wǎng)頁(yè)回退-關(guān)閉頁(yè)面 */
? ? ? ? ? ? if (customView!= null) {
? ? ? ? ? ? ? ? hideCustomView();
? ? ? ? ? ? } else if (webView.canGoBack()) {
? ? ? ? ? ? ? ? webView.goBack();
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? finish();
}
? ? ? ? ? ? return true;
? ? ? ? default:
? ? ? ? ? ? return super.onKeyDown(keyCode, event);
? ? }
}