MainActivity代碼
public class Html5VideoAutoPlay extends Activity {
WebView webview = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.html5video);
webview = (WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient(){
/**
* 當前網頁的鏈接仍在webView中跳轉
*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
/**
* 處理ssl請求
*/
@Override
public void onReceivedSslError(WebView view,
SslErrorHandler handler, SslError error) {
handler.proceed();
}
/**
* 頁面載入完成回調
*/
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:try{autoplay();}catch(e){}");
}
});
webview.setWebChromeClient(new WebChromeClient() {
/**
* 顯示自定義視圖,無此方法視頻不能播放
*/
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
}
});
webview.loadUrl("file:///sdcard/html/video.html");
}
@Override
protected void onPause() {
if(null != webview) {
webview.onPause();
}
super.onPause();
}
}
二,布局文件
html5video.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
三,html文件(這里用的是html5的video標簽來設置自動播放和循環播放)在main下創建 assets文件夾,再創建
video.html
<body>
<video id="video" src="b.mp4" width="480" height="320" controls loop>
don't support html5
</video>
</body>
<script type="text/javascript">
var video = document.getElementById("video");
video.play();
</script>
上面的src可以引入本地視頻b.mp4,
也可以引入網上視頻:http://2449.vod.myqcloud.com/2449_43b6f696980311e59ed467f22794e792.f20.mp4
完成
參考於:https://blog.csdn.net/qiushi_1990/article/details/51438198
webview 播放video視頻 如何實現自動播放
<video ref="videoAuto" width="1080px" height="1920px" loop > <source src="../../../static/image/logo.mp4" type="video/mp4"> </video> 這是自動播放的js autoVido(){ let self= this let videos = self.$refs.videoAuto; videos.loop = 'loop'; videos.play(); }
