WebView中的视频播放

activity_main.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:background="#FFFFFF"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/videoWebView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true" />

</LinearLayout>

 

实现代码:

public class MainActivity extends Activity {

	private WebView videoWebView;
	private String videoUrl = "视频地址.mp4";
	private String imgUrl = "视频封面图片地址.png";
	private String html = "<html>视频如下:<br/><a href='" + videoUrl
			+ "' width='650' height='450' frameborder='0'><img src='" + imgUrl
			+ "' /></a></html>";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		videoWebView = (WebView) findViewById(R.id.videoWebView);

		WebSettings setting = videoWebView.getSettings();
		setting.setJavaScriptEnabled(true);
		setting.setDomStorageEnabled(true);
		setting.setDatabaseEnabled(true);
		setting.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		setting.setDefaultTextEncodingName("UTF-8");

		videoWebView.setWebChromeClient(new WebChromeClient());
		videoWebView.setWebViewClient(new MyWebViewClient());
		videoWebView.loadData(html, "text/html;charset=UTF-8", "UTF-8");
	}

	public class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			if (url.endsWith(".mp4")) {
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setDataAndType(Uri.parse(url), "video/*");
				view.getContext().startActivity(intent);

				return true;
			} else {
				return super.shouldOverrideUrlLoading(view, url);
			}
		}
	}
}

 



    本文附件下载:
  • video.zip (2.1 MB)


已有 0 人发表留言,猛击->> 这里<<-参与讨论


ITeye推荐
  • —软件人才免语言低担保 赴美带薪读研!—



你可能感兴趣的:(视频,webView,播放)