android webview自动播放Video – xiangyong2008的专栏 – 博客频道 – CSDN.NET

  1. 由于近期有个需求需要在webview加载完网页上的视频标签(html5 video),经过一番查资料和尝试之后,终于实现了,具体实现如下:自定义一个xml 里面包含Webview组件,自定义WebViewClient,实现onPageFinished方法,回调javascript,实现播放功能

      

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1.   
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. package com.zr.webviewtest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.webkit.ConsoleMessage;  
  8. import android.webkit.WebChromeClient;  
  9. import android.webkit.WebResourceResponse;  
  10. import android.webkit.WebSettings;  
  11. import android.webkit.WebView;  
  12. import android.webkit.WebViewClient;  
  13. import android.widget.FrameLayout;  
  14.   
  15. import com.samsung.zr.webviewtest.R;  
  16.   
  17. public class MainActivity extends Activity {  
  18.       
  19.     private FrameLayout frameLayout = null;  
  20.     private WebView webView = null;  
  21.     private WebChromeClient chromeClient = null;  
  22.     private View myView = null;  
  23.     private WebChromeClient.CustomViewCallback myCallBack = null;  
  24.       
  25.       
  26.     public MainActivity() {  
  27.     }  
  28.       
  29.     @Override  
  30.     protected void onCreate(Bundle savedInstanceState) {  
  31.           
  32.         super.onCreate(savedInstanceState);  
  33.           
  34.         setContentView(R.layout.activity_main);  
  35.           
  36.         frameLayout = (FrameLayout)findViewById(R.id.framelayout);  
  37.         webView = (WebView)findViewById(R.id.webview);  
  38.           
  39.         webView.getSettings().setJavaScriptEnabled(true);  
  40.         webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);  
  41.           
  42.           
  43.         webView.setWebViewClient(new MyWebviewCient());  
  44.           
  45.         chromeClient = new MyChromeClient();  
  46.           
  47.         webView.setWebChromeClient(chromeClient);  
  48.         webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);  
  49.         webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);  
  50.   
  51.         webView.setHorizontalScrollBarEnabled(false);  
  52.         webView.setVerticalScrollBarEnabled(false);   
  53.           
  54.         final String USER_AGENT_STRING = webView.getSettings().getUserAgentString() + ” Rong/2.0″;  
  55.         webView.getSettings().setUserAgentString( USER_AGENT_STRING );  
  56.         webView.getSettings().setSupportZoom(false);  
  57.         webView.getSettings().setPluginState(WebSettings.PluginState.ON);  
  58.         webView.getSettings().setLoadWithOverviewMode(true);  
  59.           
  60.         webView.loadUrl(“file:///android_asset/index.html”);  
  61.           
  62.         if(savedInstanceState != null){  
  63.             webView.restoreState(savedInstanceState);  
  64.         }  
  65.           
  66.     }  
  67.       
  68.     @Override  
  69.     public void onBackPressed() {  
  70.         if(myView == null){  
  71.             super.onBackPressed();  
  72.         }  
  73.         else{  
  74.             chromeClient.onHideCustomView();  
  75.         }  
  76.     }  
  77.       
  78.     @Override  
  79.     protected void onSaveInstanceState(Bundle outState) {  
  80.         // TODO Auto-generated method stub  
  81.         webView.saveState(outState);  
  82.     }  
  83.       
  84.     public void addJavaScriptMap(Object obj, String objName){  
  85.         webView.addJavascriptInterface(obj, objName);  
  86.     }  
  87.       
  88.     public class MyWebviewCient extends WebViewClient{  
  89.         @Override  
  90.         public WebResourceResponse shouldInterceptRequest(WebView view,  
  91.                 String url) {  
  92.             WebResourceResponse response = null;  
  93.             response = super.shouldInterceptRequest(view, url);  
  94.             return response;  
  95.         }  
  96.   
  97.         @Override  
  98.         public void onPageFinished(WebView view, String url) {  
  99.             // TODO Auto-generated method stub  
  100.             super.onPageFinished(view, url);  
  101.             Log.d(“dream”“***on page finished”);  
  102.             webView.loadUrl(“javascript:myFunction()”);   
  103.         }  
  104.           
  105.     }  
  106.       
  107.     public class MyChromeClient extends WebChromeClient{  
  108.           
  109.         @Override  
  110.         public void onShowCustomView(View view, CustomViewCallback callback) {  
  111.             if(myView != null){  
  112.                 callback.onCustomViewHidden();  
  113.                 return;  
  114.             }  
  115.             frameLayout.removeView(webView);  
  116.             frameLayout.addView(view);  
  117.             myView = view;  
  118.             myCallBack = callback;  
  119.         }  
  120.           
  121.         @Override  
  122.         public void onHideCustomView() {  
  123.             if(myView == null){  
  124.                 return;  
  125.             }  
  126.             frameLayout.removeView(myView);  
  127.             myView = null;  
  128.             frameLayout.addView(webView);  
  129.             myCallBack.onCustomViewHidden();  
  130.         }  
  131.           
  132.         @Override  
  133.         public boolean onConsoleMessage(ConsoleMessage consoleMessage) {  
  134.             // TODO Auto-generated method stub  
  135.             Log.d(“ZR”, consoleMessage.message()+” at “+consoleMessage.sourceId()+“:”+consoleMessage.lineNumber());  
  136.             return super.onConsoleMessage(consoleMessage);  
  137.         }  
  138.     }  
  139.       
  140. }  

XML布局文件:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. <FrameLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  2.     xmlns:tools=“http://schemas.android.com/tools”  
  3.     android:id=“@+id/framelayout”  
  4.     android:layout_width=“match_parent”  
  5.     android:layout_height=“match_parent” >  
  6.   
  7.     <WebView  
  8.         android:id=“@+id/webview”  
  9.         android:layout_width=“match_parent”  
  10.         android:layout_height=“match_parent” />  
  11.   
  12. </FrameLayout>  

网页文件:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. <!DOCTYPE html>  
  2. <html xmlns=“http://www.w3.org/1999/xhtml”>  
  3. <head>  
  4.     <meta name=“viewport” content=“”/>  
  5.     <meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”/>  
  6.     <title>FamilyAlbum</title>  
  7.       
  8.     <link rel=“stylesheet” type=“text/css” href=“css/main.css”  media=“all”/>  
  9.   
  10.     <script type=“text/javascript” src=“lib/jquery-1.7.2.min.js”> </script>  
  11.     <script type=“text/javascript” src=“lib/jquery-ui-1.8.23.custom.min.js”> </script>  
  12.     <script type=“text/javascript” src=“lib/jquery.confirm.js”> </script>        
  13.     <script type=“text/javascript” src=“js/main.js”> </script>  
  14.       
  15. <script type=“text/javascript”>  
  16.     function myFunction()  
  17.     {  
  18.         //document.getElementById(“demo”).innerHTML=“My First JavaScript Function”;  
  19.         var videos = document.getElementById(‘videoTag’);   
  20.         videos.loop = ‘loop’;  
  21.         videos.play();  
  22.           
  23.     }  
  24. </script>  
  25. </head>  
  26.   
  27. <body>  
  28.     <video id=“videoTag” src=“file:///storage/emulated/0/big_buck_bunny.mp4” controls=“controls” height=“210px” width=“380px”></video>  
  29.   
  30. <button type=“button” onclick=“myFunction()”>点击这里</button>  
  31.   
  32. <p id=“demo”>A Paragraph.</p>  
  33.   
  34. </body>  
  35. </html>  

源码下载地址: http://download.csdn.net/detail/xiangyong2008/7473159 

来源URL:http://blog.csdn.net/xiangyong2008/article/details/29581781