Android中webview html5 自動播放本地視頻

MainActivity代碼javascript

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.xmlhtml

<?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.htmlhtml5

<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.mp4java

完成android

參考於:https://blog.csdn.net/qiushi_1990/article/details/51438198web

 

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(); }
相關文章
相關標籤/搜索