因为公司用的app没有用到webView去展示页面,然后一天一个朋友问我webView中怎么让h5页面的视频点击全屏的时候播放,当时还真的懵逼了。后来在网上查资料,还是找到不错的文章。
首先支持原创:http://www.jianshu.com/p/2dd60e6bd798
webView全屏播放视频的步骤:
1、启动硬件加速
2、webView控制所在的XML文件内容
3、Activity中初始化webView
4、自定义WebChromeClient类
5、自定义WebViewClient类
6、在Acitivity中复写onConfigurationChanged方法
7、Activity中声明周期内webView的操作
图直接拿过来用了,因为我也是用他的这个资源的
1、启动硬件加速
在AndroidManifest.xml中声明使用HardwareAccelerate,请测没有开启只有声音没有画面(在application和activity都声明了)。以及配置configChanges,在横屏情况下不设置会导致activity销毁重新创建,记得添加权限。差点忘了
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".base.MyApplication"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:hardwareAccelerated ="true"
android:theme="@style/AppTheme">
<activity android:name=".Activity.ScreenWebActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:hardwareAccelerated="true"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
这里主题也贴出来吧,application的主题是默认的就好了
activity的主题
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
3、在activity中初始化webView
注意:去掉标题,否则可能无法全屏
activity_screen_webview
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/mFrameLayout">
<WebView
android:id="@+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20sp" />
</FrameLayout>
初始化webView
private FrameLayout mFrameLayout;
private WebView mWebView;
private InsideWebChromeClient mInsideWebChromeClient;
//需要加载的h5页面url地址
private String URL = "http://m.tv.sohu.com/20140508/n399272261.shtml";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_screen_webview);
mFrameLayout = (FrameLayout) findViewById(R.id.mFrameLayout);
mWebView = (WebView) findViewById(R.id.webView);
initWebView();
mWebView.loadUrl(URL);
}
private void initWebView() {
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);
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
mInsideWebChromeClient = new InsideWebChromeClient();
InsideWebViewClient mInsideWebViewClient = new InsideWebViewClient();
//javascriptInterface = new JavascriptInterface();
//mWebView.addJavascriptInterface(javascriptInterface, "java2js_laole918");
mWebView.setWebChromeClient(mInsideWebChromeClient);
mWebView.setWebViewClient(mInsideWebViewClient);
}
自定义webChromeClient类
思路:在onShowCustomView方法中,将获取到的view放入到当前activity的最上方,在onHideCustomerView中,将之前的View隐藏或者删除,将原来被覆盖的webView放回来,并播放结束。
private class InsideWebChromeClient extends WebChromeClient {
private View mCustomView;
private CustomViewCallback mCustomViewCallback;
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mCustomView = view;
mFrameLayout.addView(mCustomView);
mCustomViewCallback = callback;
mWebView.setVisibility(View.GONE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
public void onHideCustomView() {
mWebView.setVisibility(View.VISIBLE);
if (mCustomView == null) {
return;
}
mCustomView.setVisibility(View.GONE);
mFrameLayout.removeView(mCustomView);
mCustomViewCallback.onCustomViewHidden();
mCustomView = null;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onHideCustomView();
}
}
5、自定义webViewClient类
重写webViewClient在onPageFinished()方法中可以注入js语句。
private class InsideWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
6、在activity中复写onConfigurationChanged()方法
@Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
switch (config.orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
break;
case Configuration.ORIENTATION_PORTRAIT:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
break;
}
}
7、在Activity中声明周期内的webView操作
@Override
public void onPause() {
super.onPause();
mWebView.onPause();
}
@Override
public void onResume() {
super.onResume();
mWebView.onResume();
}
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
return;
}
super.onBackPressed();
}
@Override
public void onDestroy() {
mWebView.destroy();
super.onDestroy();
}
完整的activity
screenWebActivity
public class ScreenWebActivity extends AppCompatActivity {
private FrameLayout mFrameLayout;
private WebView mWebView;
private InsideWebChromeClient mInsideWebChromeClient;
//需要加载的h5页面url地址
private String URL = "http://m.tv.sohu.com/20140508/n399272261.shtml";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_screen_webview);
mFrameLayout = (FrameLayout) findViewById(R.id.mFrameLayout);
mWebView = (WebView) findViewById(R.id.webView);
initWebView();
mWebView.loadUrl(URL);
}
private void initWebView() {
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);
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
mInsideWebChromeClient = new InsideWebChromeClient();
InsideWebViewClient mInsideWebViewClient = new InsideWebViewClient();
//javascriptInterface = new JavascriptInterface();
//mWebView.addJavascriptInterface(javascriptInterface, "java2js_laole918");
mWebView.setWebChromeClient(mInsideWebChromeClient);
mWebView.setWebViewClient(mInsideWebViewClient);
}
private class InsideWebChromeClient extends WebChromeClient {
private View mCustomView;
private CustomViewCallback mCustomViewCallback;
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mCustomView = view;
mFrameLayout.addView(mCustomView);
mCustomViewCallback = callback;
mWebView.setVisibility(View.GONE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
public void onHideCustomView() {
mWebView.setVisibility(View.VISIBLE);
if (mCustomView == null) {
return;
}
mCustomView.setVisibility(View.GONE);
mFrameLayout.removeView(mCustomView);
mCustomViewCallback.onCustomViewHidden();
mCustomView = null;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onHideCustomView();
}
}
private class InsideWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
@Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
switch (config.orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
break;
case Configuration.ORIENTATION_PORTRAIT:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
break;
}
}
@Override
public void onPause() {
super.onPause();
mWebView.onPause();
}
@Override
public void onResume() {
super.onResume();
mWebView.onResume();
}
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
return;
}
super.onBackPressed();
}
@Override
public void onDestroy() {
mWebView.destroy();
super.onDestroy();
}
}
如果没有正确的全屏
1、可能是configChanges这个写错了属性
## 犯错的错误写成了如下:
android:configChanges="orientation|keyboardHidden" -->
## 正确的写法:
android:configChanges="orientation|screenSize|keyboardHidden"
2、主题一定的是NoActionBar
//本人犯得错误是,虽然写了requestWindow,但是还是没有改变,然后就修改了主题,设置成了noActionBar
requestWindowFeature(Window.FEATURE_NO_TITLE);
最后还是要感谢下原创者,因为不常用很容易忘记,到时候就方便复习了,非常感谢。