WebView中的视频播放

标签: webview 视频 播放 | 发表时间:2014-06-27 21:12 | 作者:jsntghf
出处:http://www.iteye.com

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);
			}
		}
	}
}

 





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


ITeye推荐



相关 [webview 视频 播放] 推荐:

WebView中的视频播放

- - 移动开发 - ITeye博客
activity_main.xml如下:. private String videoUrl = "视频地址.mp4";. private String imgUrl = "视频封面图片地址.png";. private String html = "视频如下:
应用开发托管服务';