Android的WebView中H5页面全屏播放视频

企业动态
如何让WebView中H5页面全屏播放视频。关于这个问题,做一下简单分析,希望对你有所帮助。

如何让WebView中H5页面全屏播放视频。关于这个问题,做一下简单分析,希望对你有所帮助。

和往常一样,我们先来看效果:

[[190652]]

运行效果

其实很简单,就是配置问题。关键地方配好了,基本没什么问题了。

  • 硬件加速
  • 设置WebView

在清单需要配置的AndroidManifest.xml

  1. <application 
  2.     android:allowBackup="true" 
  3.     android:icon="@mipmap/ic_launcher" 
  4.     android:label="@string/app_name" 
  5.     android:supportsRtl="true" 
  6.     android:theme="@style/AppTheme"
  7.     <activity 
  8.         android:name=".WebViewActivity" 
  9.         android:configChanges="orientation|screenSize|keyboardHidden" 
  10.         android:hardwareAccelerated="true"
  11.         <intent-filter> 
  12.           <action android:name="android.intent.action.MAIN" /> 
  13.           <category android:name="android.intent.category.LAUNCHER" /> 
  14.         </intent-filter> 
  15.     </activity> 
  16. </application> 

这里需要注意的是启动硬件加速可以 在application 启动这个硬件加速,也可以在对应的activity启动

  1. android:hardwareAccelerated="true" 

还有这个

  1. configChanges="orientation|screenSize|keyboardHidden" 

必须是

  1. orientation|screenSize|keyboardHidden 

当然记得加上网络权限

  1. <uses-permission android:name="android.permission.INTERNET" /> 

下面给出全部源码

activity_webview.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:id="@+id/mFrameLayout" 
  4.     android:layout_width="match_parent" 
  5.     android:layout_height="match_parent"
  6.  
  7.     <WebView 
  8.         android:id="@+id/mWebView" 
  9.         android:layout_width="match_parent" 
  10.         android:layout_height="match_parent" /> 
  11. </FrameLayout> 

WebViewActivity源码

  1. public class WebViewActivity extends AppCompatActivity  { 
  2.     private FrameLayout mFrameLayout; 
  3.     private WebView mWebView; 
  4.     private MyWebChromeClient mMyWebChromeClient; 
  5.     private String URL = "http://m.tv.sohu.com/20130704/n380744170.shtml"
  6.     @Override 
  7.     protected void onCreate(Bundle savedInstanceState) { 
  8.         requestWindowFeature(Window.FEATURE_NO_TITLE); 
  9.         super.onCreate(savedInstanceState); 
  10.         setContentView(R.layout.activity_webview); 
  11.         mFrameLayout = (FrameLayout) findViewById(R.id.mFrameLayout); 
  12.         mWebView = (WebView) findViewById(R.id.mWebView); 
  13.         initWebView(); 
  14.         mWebView.loadUrl(URL); 
  15.     } 
  16.     private void initWebView() { 
  17.         WebSettings settings = mWebView.getSettings(); 
  18.         settings.setJavaScriptEnabled(true); 
  19.         settings.setJavaScriptCanOpenWindowsAutomatically(true); 
  20.         settings.setPluginState(WebSettings.PluginState.ON); 
  21.         settings.setAllowFileAccess(true); 
  22.         settings.setLoadWithOverviewMode(true); 
  23.         settings.setUseWideViewPort(true); 
  24.         settings.setCacheMode(WebSettings.LOAD_NO_CACHE); 
  25.         settings.setCacheMode(WebSettings.LOAD_DEFAULT); 
  26.         mMyWebChromeClient = new MyWebChromeClient(); 
  27.         mWebView.setWebChromeClient(mMyWebChromeClient); 
  28.         mWebView.setWebViewClient(new WebViewClient() { 
  29.             public boolean shouldOverrideUrlLoading(WebView view, String url) { 
  30.                 view.loadUrl(url); 
  31.                 return true
  32.             } 
  33.  
  34.             @Override 
  35.             public void onPageFinished(WebView view, String url) { 
  36.                 super.onPageFinished(view, url); 
  37.             } 
  38.         }); 
  39.     } 
  40.  
  41.     private class MyWebChromeClient extends WebChromeClient { 
  42.         private View mCustomView; 
  43.         private CustomViewCallback mCustomViewCallback; 
  44.         @Override 
  45.         public void onShowCustomView(View view, CustomViewCallback callback) { 
  46.             super.onShowCustomView(view, callback); 
  47.             if (mCustomView != null) { 
  48.                 callback.onCustomViewHidden(); 
  49.                 return
  50.             } 
  51.             mCustomView = view
  52.             mFrameLayout.addView(mCustomView); 
  53.             mCustomViewCallback = callback; 
  54.             mWebView.setVisibility(View.GONE); 
  55.             setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
  56.         } 
  57.  
  58.         public void onHideCustomView() { 
  59.             mWebView.setVisibility(View.VISIBLE); 
  60.             if (mCustomView == null) { 
  61.                 return
  62.             } 
  63.             mCustomView.setVisibility(View.GONE); 
  64.             mFrameLayout.removeView(mCustomView); 
  65.             mCustomViewCallback.onCustomViewHidden(); 
  66.             mCustomView = null
  67.             setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
  68.             super.onHideCustomView(); 
  69.         } 
  70.     } 
  71.     @Override 
  72.     public void onConfigurationChanged(Configuration config) { 
  73.         super.onConfigurationChanged(config); 
  74.         switch (config.orientation) { 
  75.             case Configuration.ORIENTATION_LANDSCAPE: 
  76.                 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
  77.                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
  78.                 break; 
  79.             case Configuration.ORIENTATION_PORTRAIT: 
  80.                 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
  81.                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
  82.                 break; 
  83.         } 
  84.     } 
  85.     @Override 
  86.     public void onPause() { 
  87.         super.onPause(); 
  88.         mWebView.onPause(); 
  89.     } 
  90.  
  91.     @Override 
  92.     public void onResume() { 
  93.         super.onResume(); 
  94.         mWebView.onResume(); 
  95.     } 
  96.  
  97.     @Override 
  98.     public void onBackPressed() { 
  99.         if (mWebView.canGoBack()) { 
  100.             mWebView.goBack(); 
  101.             return
  102.         } 
  103.         super.onBackPressed(); 
  104.     } 
  105.  
  106.     @Override 
  107.     public void onDestroy() { 
  108.         super.onDestroy(); 
  109.         mWebView.destroy(); 
  110.     } 

 【本文为51CTO专栏作者“洪生鹏”的原创稿件,转载请联系原作者】

戳这里,看该作者更多好文

责任编辑:武晓燕 来源: 51CTO专栏
相关推荐

2013-08-13 14:08:23

android 4.0WebView

2022-04-28 15:38:42

WebViewJavaUI

2020-12-13 12:14:45

H5开发H5-Dooring

2023-03-29 09:37:49

视频播放器应用鸿蒙

2015-07-15 14:38:54

H5移动适配

2015-07-15 12:30:37

移动端H5高清多屏

2024-04-10 08:24:29

2023-08-15 08:34:28

页面制作工具GitHub

2021-06-08 05:53:31

H5 页面项目刘海屏适配

2024-04-11 10:02:06

iOS键盘Android

2023-05-29 18:33:30

得物H5容器

2019-12-05 16:10:37

云计算行业科技

2022-06-27 09:48:15

H5移动互联网页面性能

2018-08-01 15:49:51

AndroidH5通信

2020-06-04 16:57:07

移动开发互联网实践

2022-03-28 07:52:31

H5小游戏开发教程页面基础布局

2017-08-16 10:57:25

H5HTML开发

2015-09-21 13:22:50

Cocos手机页游

2018-02-06 16:21:13

H5首屏探讨

2014-09-26 11:29:23

微信浏览
点赞
收藏

51CTO技术栈公众号