android播放视频

来源:互联网 发布:关联分析算法的优点 编辑:程序博客网 时间:2024/06/06 00:31
Java代码  收藏代码
  1. WebView webView = (WebView) findViewById(R.id.webView);  
  2. webView.setDownloadListener(new DownloadListener() {  
  3.               
  4.             @Override  
  5.             public void onDownloadStart(String url, String userAgent,  
  6.                     String contentDisposition, String mimetype, long contentLength) {  
  7.                 Intent intent = new Intent(Intent.ACTION_VIEW);  
  8.                 intent.setDataAndType(Uri.parse(url), mimetype);  
  9.                 try {  
  10.                     startActivity(intent);  
  11.                 }catch (ActivityNotFoundException e){  
  12.                     Log.w("YourLogTag""Couldn't find activity to view mimetype: " + mimetype);  
  13.                     e.printStackTrace();  
  14.                 } catch (Exception e) {  
  15.                     e.printStackTrace();  
  16.                 }  
  17.             }  
  18.         });  
  19. webView.setWebViewClient(new WebViewClient(){  
  20.   
  21. public boolean shouldOverrideUrlLoading(Webview view, String url){  
  22.     if(url.endsWith(".mp4") || url.endsWith("some other supported type")){  
  23.         Intent i = new Intent(Intent.ACTION_VIEW);  
  24.         i.setData(Uri.parse(url));  
  25.         startActivity(i); //warning no error handling will cause force close if no media player on phone.  
  26.         return true;  
  27.     } else {  
  28.         view.loadUrl(url);  
  29.     }  
  30.     return true;  
  31. }});  


WebView退出后flash视频播放器无法退出的问题 . 
转自:http://blog.csdn.net/a345017062/article/details/6788502 
Java代码  收藏代码
  1. public void onPause() {//继承自Activity  
  2.     super.onPause();  
  3.     webView.onPause();  
  4. }  
  5. public void onResume() {//继承自Activity  
  6.     super.onResume();  
  7.     webView.onResume();  
  8. }  

把这两个加上就可以了。 

另外,看到网上有提到有下面这种方式: 
Java代码  收藏代码
  1. webView.pauseTimers();  
  2. webView.stopLoading();  
  3. webView.loadData("<a></a>""text/html""utf-8");  

这样直接就把视频停掉无法恢复了。 

转自:http://aichixihongshi.iteye.com/blog/1188345

0 0
原创粉丝点击