近来做一个应用,遇到了这样的问题,介绍一下场景吧:
从论坛上抓取了帖子的内容,放到webview中显示。其中可能包含网络视频。视频控件采用的是<embed />标签。
如何达到标题介绍的效果呢?我们一步步来看:
1、设置webview属性:
WebSettings setting = wv.getSettings();
setting.setJavaScriptEnabled(true);
setting.setPluginState(PluginState.ON);
这样就可以播放了。但是有的网络视频自带全屏按钮,这是就要进行相应的设置,否则点击后会崩溃。
2、设置WebChromeClient:
class MyWebChromeClient extends WebChromeClient {
private CustomViewCallback customViewCallback;
private int mOriginalOrientation = 1;
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
onShowCustomView(view, mOriginalOrientation, callback);
super.onShowCustomView(view, callback);
}
public void onShowCustomView(View view, int requestedOrientation,
WebChromeClient.CustomViewCallback callback) {
if (customView != null) {
callback.onCustomViewHidden();
return;
}
customView = view;
customViewCallback = callback;
mOriginalOrientation = getRequestedOrientation();
ll_content.setVisibility(View.GONE);
fl_video.addView(customView);
fl_video.setVisibility(View.VISIBLE);
fl_video.bringToFront();
//设置横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//设置全屏
getParent().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
public void onHideCustomView() {
ll_content.setVisibility(View.VISIBLE);
if (customView == null) {
return;
}
fl_video.removeView(customView);
customView = null;
fl_video.setVisibility(View.GONE);
try {
customViewCallback.onCustomViewHidden();
} catch (Exception e) {
}
// 设置竖屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// 取消全屏
final WindowManager.LayoutParams attrs = getParent().getWindow()
.getAttributes();
attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getParent().getWindow().setAttributes(attrs);
getParent().getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
}
customVuew 是一个View对象。
此处需要注意的是我当前的Activity是一个ActivityGroup的子Activity,所以需要getParent().getWindow()……,如果是单独的Activity,则去掉getParent()。奇怪的是如果不设置全屏的话不能正常播放。
布局文件:<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#888888"
>
<FrameLayout
android:id="@+id/fl_video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:visibility="gone" />
<LinearLayout
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/wv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
</FrameLayout>
3、针对横竖屏转换所造成的配置更改进行如下设置:
在AndroidManifest.xml文件中对当前Activity及ActivityGroup进行设置
android:configChanges="orientation|keyboardHidden|navigation|screenSize"
该语句使得横竖屏转换不会进行配置更改。
这样就达到了标题所示的目的。。。刚刚整理了一个Demo,需要下载的朋友可以看看: