目录
- 介绍
- 主要代码
介绍
最近项目开发中用到了webview播放视频的功能,总结了开发中犯过的错误,这些错误在开发是及容易遇到的,所以我这里总结了一下,希望大家看到后不要再犯类似的错误,尽可能提高开发效率:
这个demo我这里也参考了网上写的一个比较好的一个demo,经过总结修改,写出来的。
主要代码
以下是相应代码:
mainactivity:
package com.androidwebviewdemo;
import android.app.activity;
import android.app.progressdialog;
import android.content.pm.activityinfo;
import android.os.bundle;
import android.util.log;
import android.view.keyevent;
import android.view.layoutinflater;
import android.view.view;
import android.view.window;
import android.view.windowmanager;
import android.webkit.webchromeclient;
import android.webkit.webchromeclient.customviewcallback;
import android.webkit.websettings;
import android.webkit.webview;
import android.webkit.webviewclient;
import android.widget.framelayout;
/**
* 使用webview播放视频时需要注意的地方:
* 1、加网络访问权限(及其他所需要的权限);
* 2、webviewclient中方法shouldoverrideurlloading可用来实现点击webview页面的链接;
* 3、webview中播放视频需要添加webview.setwebchromeclient(new webchromeclient());
* 4、视频竖屏时,点击全屏,想要切换到横屏全屏的状态,那么必须在manifest.xml配置文件该activity的
* 配置文件中添加android:configchanges="orientation|screensize"语句。
* 5、如果视频不能播放,或者播放比较卡,可以采用硬件加速,即在application,或所在的activity的配置文件中添加
* android:hardwareaccelerated="true"即可。
* @author zhongyao
*/
public class mainactivity extends activity {
private webview webview;
private framelayout video_fullview;// 全屏时视频加载view
private view xcustomview;
private progressdialog waitdialog = null;
private customviewcallback xcustomviewcallback;
private mywebchromeclient xwebchromeclient;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
requestwindowfeature(window.feature_no_title);// 去掉应用标题
getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,
windowmanager.layoutparams.flag_fullscreen);
setcontentview(r.layout.activity_main);
waitdialog = new progressdialog(this);
waitdialog.settitle("提示");
waitdialog.setmessage("视频页面加载中...");
waitdialog.setindeterminate(true);
waitdialog.setcancelable(true);
waitdialog.show();
webview = (webview) findviewbyid(r.id.webview);
video_fullview = (framelayout) findviewbyid(r.id.video_fullview);
websettings ws = webview.getsettings();
ws.setbuiltinzoomcontrols(true);// 隐藏缩放按钮
// ws.setlayoutalgorithm(websettings.layoutalgorithm.normal);// 排版适应屏幕
ws.setusewideviewport(true);// 可任意比例缩放
ws.setloadwithoverviewmode(true);// setusewideviewport方法设置webview推荐使用的窗口。setloadwithoverviewmode方法是设置webview加载的页面的模式。
ws.setsavepassword(true);
ws.setsaveformdata(true);// 保存表单数据
ws.setjavascriptenabled(true);
ws.setgeolocationenabled(true);// 启用地理定位
ws.setgeolocationdatabasepath("/data/data/org.itri.html5webview/databases/");// 设置定位的数据库路径
ws.setdomstorageenabled(true);
ws.setsupportmultiplewindows(true);// 新加
xwebchromeclient = new mywebchromeclient();
webview.setwebchromeclient(xwebchromeclient);
webview.setwebviewclient(new mywebviewclient());
webview.loadurl("http://look.appjx.cn/mobile_api.php?mod=news&id=12604");
}
public class mywebviewclient extends webviewclient {
@override
public boolean shouldoverrideurlloading(webview view, string url) {
view.loadurl(url);
return false;
}
@override
public void onpagefinished(webview view, string url) {
super.onpagefinished(view, url);
waitdialog.dismiss();
}
}
public class mywebchromeclient extends webchromeclient {
private view xprogressvideo;
// 播放网络视频时全屏会被调用的方法
@override
public void onshowcustomview(view view, customviewcallback callback) {
setrequestedorientation(activityinfo.screen_orientation_landscape);
webview.setvisibility(view.invisible);
// 如果一个视图已经存在,那么立刻终止并新建一个
if (xcustomview != null) {
callback.oncustomviewhidden();
return;
}
video_fullview.addview(view);
xcustomview = view;
xcustomviewcallback = callback;
video_fullview.setvisibility(view.visible);
}
// 视频播放退出全屏会被调用的
@override
public void onhidecustomview() {
if (xcustomview == null)// 不是全屏播放状态
return;
setrequestedorientation(activityinfo.screen_orientation_portrait);
xcustomview.setvisibility(view.gone);
video_fullview.removeview(xcustomview);
xcustomview = null;
video_fullview.setvisibility(view.gone);
xcustomviewcallback.oncustomviewhidden();
webview.setvisibility(view.visible);
}
// 视频加载时进程loading
@override
public view getvideoloadingprogressview() {
if (xprogressvideo == null) {
layoutinflater inflater = layoutinflater
.from(mainactivity.this);
xprogressvideo = inflater.inflate(
r.layout.video_loading_progress, null);
}
return xprogressvideo;
}
}
/**
* 判断是否是全屏
*
* @return
*/
public boolean incustomview() {
return (xcustomview != null);
}
/**
* 全屏时按返加键执行退出全屏方法
*/
public void hidecustomview() {
xwebchromeclient.onhidecustomview();
setrequestedorientation(activityinfo.screen_orientation_portrait);
}
@override
protected void onresume() {
super.onresume();
super.onresume();
webview.onresume();
webview.resumetimers();
/**
* 设置为横屏
*/
if (getrequestedorientation() != activityinfo.screen_orientation_landscape) {
setrequestedorientation(activityinfo.screen_orientation_portrait);
}
}
@override
protected void onpause() {
super.onpause();
webview.onpause();
webview.pausetimers();
}
@override
protected void ondestroy() {
super.ondestroy();
super.ondestroy();
video_fullview.removeallviews();
webview.loadurl("about:blank");
webview.stoploading();
webview.setwebchromeclient(null);
webview.setwebviewclient(null);
webview.destroy();
webview = null;
}
@override
public boolean onkeydown(int keycode, keyevent event) {
if (keycode == keyevent.keycode_back) {
if (incustomview()) {
// webviewdetails.loadurl("about:blank");
hidecustomview();
return true;
} else {
webview.loadurl("about:blank");
mainactivity.this.finish();
}
}
return false;
}
}
activity_main.xml:
<linearlayout 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" >
<framelayout
android:id="@+id/video_fullview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone" >
</framelayout>
<webview
android:id="@+id/webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margintop="20sp" />
</linearlayout>
video_loading_progress.xml:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progress_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerinparent="true"
android:orientation="vertical" >
<progressbar
android:id="@android:id/progress"
style="?android:attr/progressbarstylelarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingtop="5dip"
android:text="正在玩命加载视频中。。。"
android:textcolor="?android:attr/textcolorprimary"
android:textsize="14sp" />
</linearlayout>
androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidwebviewdemo"
android:versioncode="1"
android:versionname="1.0" >
<uses-sdk
android:minsdkversion="8"
android:targetsdkversion="17" />
<uses-permission android:name="android.permission.internet" />
<uses-permission android:name="android.permission.access_coarse_location" />
<uses-permission android:name="android.permission.access_fine_location" />
<uses-permission android:name="android.permission.access_mock_location" />
<uses-permission android:name="android.permission.access_gps" />
<uses-permission android:name="android.permission.access_assisted_gps" />
<uses-permission android:name="android.permission.access_location" />
<uses-permission android:name="android.permission.read_phone_state"/>
<application
android:allowbackup="true"
android:hardwareaccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/apptheme" >
<!-- android:configchanges="orientation|keyboardhidden" -->
<!-- 默认竖屏,点击全屏后再横屏,
那么activity必须配置android:configchanges="orientation|screensize"
这样一来,旋转屏幕,只会调用onconfigurationchanged,不会创建新activity。
不然的话,代码中设置横屏的时候,都会新建一个activity,
那样就没办法实现点击就横屏全屏了。 -->
<activity
android:name="com.androidwebviewdemo.mainactivity"
android:configchanges="orientation|screensize"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.main" />
<category android:name="android.intent.category.launcher" />
</intent-filter>
</activity>
</application>
</manifest>
以上就是android webview实现全屏播放视频的详细内容,更多关于android webview播放视频的资料请关注www.887551.com其它相关文章!