Android如何让WebView中的HTML5页面实现视频全屏播放
程序员文章站
2023-11-13 13:02:16
前言
本文主要是将最近工作中遇到的一个问题进行总结分享,主要介绍的是如何让webview中h5页面全屏播放视频。关于这个问题,做一下简单分析,希望对大家有所帮助,下面话不...
前言
本文主要是将最近工作中遇到的一个问题进行总结分享,主要介绍的是如何让webview中h5页面全屏播放视频。关于这个问题,做一下简单分析,希望对大家有所帮助,下面话不多说了,来看看详细的介绍吧。
效果图
运行效果
其实很简单,就是配置问题。关键地方配好了,基本没什么问题了。
- 硬件加速
- 设置webview
在清单需要配置的androidmanifest.xml
<application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".webviewactivity" android:configchanges="orientation|screensize|keyboardhidden" android:hardwareaccelerated="true"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application>
这里需要注意的是:启动硬件加速可以 在application 启动这个硬件加速,也可以在对应的activity启动
android:hardwareaccelerated="true"
还有这个
configchanges="orientation|screensize|keyboardhidden"
必须是
orientation|screensize|keyboardhidden
当然记得加上网络权限
<uses-permission android:name="android.permission.internet" />
下面给出全部源码
activity_webview.xml
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mframelayout" android:layout_width="match_parent" android:layout_height="match_parent"> <webview android:id="@+id/mwebview" android:layout_width="match_parent" android:layout_height="match_parent" /> </framelayout>
webviewactivity源码
public class webviewactivity extends appcompatactivity { private framelayout mframelayout; private webview mwebview; private mywebchromeclient mmywebchromeclient; private string url = "http://m.tv.sohu.com/20130704/n380744170.shtml"; @override protected void oncreate(bundle savedinstancestate) { requestwindowfeature(window.feature_no_title); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_webview); mframelayout = (framelayout) findviewbyid(r.id.mframelayout); mwebview = (webview) findviewbyid(r.id.mwebview); initwebview(); mwebview.loadurl(url); } private void initwebview() { websettings settings = mwebview.getsettings(); settings.setjavascriptenabled(true); settings.setjavascriptcanopenwindowsautomatically(true); settings.setpluginstate(websettings.pluginstate.on); settings.setallowfileaccess(true); settings.setloadwithoverviewmode(true); settings.setusewideviewport(true); settings.setcachemode(websettings.load_no_cache); settings.setcachemode(websettings.load_default); mmywebchromeclient = new mywebchromeclient(); mwebview.setwebchromeclient(mmywebchromeclient); mwebview.setwebviewclient(new webviewclient() { public boolean shouldoverrideurlloading(webview view, string url) { view.loadurl(url); return true; } @override public void onpagefinished(webview view, string url) { super.onpagefinished(view, url); } }); } private class mywebchromeclient 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(); } } @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() { super.ondestroy(); mwebview.destroy(); } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对各位android开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
上一篇: JS基础随笔(菜鸟必看篇)