WebView中的视频播放
- - 移动开发 - ITeye博客activity_main.xml如下:. private String videoUrl = "视频地址.mp4";. private String imgUrl = "视频封面图片地址.png";. private String html = "视频如下:
应用开发托管服务';
应用开发托管服务';
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); } } } }