自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

android 4.0以上WebView不能全屏播放視頻的解決辦法

移動(dòng)開(kāi)發(fā) Android
上次鄙人做了一個(gè)簡(jiǎn)單的利用webView實(shí)現(xiàn)的一個(gè)瀏覽器!其中遇到了兩個(gè)問(wèn)題,一個(gè)是將瀏覽器中需要下載的內(nèi)容托管到系統(tǒng)默認(rèn)的下載程序進(jìn)行下載。

上次鄙人做了一個(gè)簡(jiǎn)單的利用webView實(shí)現(xiàn)的一個(gè)瀏覽器!其中遇到了兩個(gè)問(wèn)題,一個(gè)是將瀏覽器中需要下載的內(nèi)容托管到系統(tǒng)默認(rèn)的下載程序進(jìn)行下載,這個(gè)比較簡(jiǎn)單就不在這里討論了;另一個(gè)問(wèn)題就是我們的Android設(shè)備版本是4.0.3,不能像Android2.3那樣支持全屏播放視頻,這個(gè)問(wèn)題比較糾結(jié),但是經(jīng)過(guò)不斷的摸索,終于解決了這個(gè)問(wèn)題。在這里和大家分享一下解決方法:

 

1、首先定義一個(gè)VideoEnabledWebView繼承自WebView,復(fù)寫(xiě)其中的loadData,loadDataWithBaseURL,loadUrl方法,道理很簡(jiǎn)單就是在加載url或者js的時(shí)候初始化一些內(nèi)容。見(jiàn)代碼:

  1. package com.danielme.android.webviewdemo; 
  2.  
  3. import java.util.Map; 
  4. import android.annotation.SuppressLint; 
  5. import android.content.Context; 
  6. import android.os.Handler; 
  7. import android.os.Looper; 
  8. import android.util.AttributeSet; 
  9. import android.webkit.WebChromeClient; 
  10. import android.webkit.WebView; 
  11.  
  12. public class VideoEnabledWebView extends WebView 
  13.     public interface ToggledFullscreenCallback 
  14.     { 
  15.         public void toggledFullscreen(boolean fullscreen); 
  16.     }     
  17.     private VideoEnabledWebChromeClient videoEnabledWebChromeClient; 
  18.     private boolean addedJavascriptInterface;    
  19.     public VideoEnabledWebView(Context context) 
  20.     { 
  21.         super(context); 
  22.         addedJavascriptInterface = false
  23.     }    
  24.     public VideoEnabledWebView(Context context, AttributeSet attrs) 
  25.     { 
  26.         super(context, attrs); 
  27.         addedJavascriptInterface = false
  28.     }    
  29.     public VideoEnabledWebView(Context context, AttributeSet attrs, int defStyle) 
  30.     { 
  31.         super(context, attrs, defStyle); 
  32.         addedJavascriptInterface = false
  33.     }     
  34.     /** 
  35.      * Pass only a VideoEnabledWebChromeClient instance. 
  36.      */ 
  37.     @Override 
  38.     @SuppressLint ("SetJavaScriptEnabled"
  39.     public void setWebChromeClient(WebChromeClient client) 
  40.     { 
  41.         getSettings().setJavaScriptEnabled(true); 
  42.  
  43.         if (client instanceof VideoEnabledWebChromeClient) 
  44.         { 
  45.             this.videoEnabledWebChromeClient = (VideoEnabledWebChromeClient) client; 
  46.         } 
  47.  
  48.         super.setWebChromeClient(client); 
  49.     }     
  50.     @Override 
  51.     public void loadData(String data, String mimeType, String encoding) 
  52.     { 
  53.         addJavascriptInterface(); 
  54.         super.loadData(data, mimeType, encoding); 
  55.     }     
  56.     @Override 
  57.     public void loadDataWithBaseURL(String baseUrl, String data, 
  58.                                     String mimeType, String encoding, 
  59.                                     String historyUrl) 
  60.     { 
  61.         addJavascriptInterface(); 
  62.         super.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl); 
  63.     }     
  64.     @Override 
  65.     public void loadUrl(String url) 
  66.     { 
  67.         addJavascriptInterface(); 
  68.         super.loadUrl(url); 
  69.     }     
  70.     @Override 
  71.     public void loadUrl(String url, Map<String, String> additionalHttpHeaders) 
  72.     { 
  73.         addJavascriptInterface(); 
  74.         super.loadUrl(url, additionalHttpHeaders); 
  75.     }     
  76.     private void addJavascriptInterface() 
  77.     { 
  78.         System.out.println(addedJavascriptInterface); 
  79.         if (!addedJavascriptInterface) 
  80.         { 
  81.             // Add javascript interface to be called when the video ends (must be done before page load) 
  82.             addJavascriptInterface(new Object() 
  83.             { 
  84.             }, "_VideoEnabledWebView"); // Must match Javascript interface name of VideoEnabledWebChromeClient             
  85.             addedJavascriptInterface = true
  86.         } 
  87.     }    
  88.   

其中addJavascriptInterface方法是將一個(gè)當(dāng)前的java對(duì)象綁定到一個(gè)javascript上面,使用如下方法

 

webv.addJavascriptInterface(this, "_VideoEnabledWebView");//this為當(dāng)前對(duì)象,綁定到j(luò)s的_VideoEnabledWebView上面,主要_VideoEnabledWebView的作用域是全局的。這個(gè)部分的內(nèi)容我不是很懂,提供鏈接給大家學(xué)習(xí)下,希望看懂的朋友能教教這個(gè)步驟是干嘛的?。?a rel="nofollow" target="_blank">http://www.oschina.net/code/snippet_232612_8531)

 

2、定義一個(gè)類VideoEnabledWebChromeClient繼承自WebChromeClient,這個(gè)WebChromeClient中的onShowCustomView方法就是播放網(wǎng)絡(luò)視頻時(shí)會(huì)被調(diào)用的方法,onHideCustomView方法就是視頻播放完成會(huì)被調(diào)用的。其中有個(gè)構(gòu)造函數(shù)需要提出來(lái):

  1. public VideoEnabledWebChromeClient(View activityNonVideoView, ViewGroup activityVideoView, View loadingView, VideoEnabledWebView webView) 
  2.     { 
  3.         this.activityNonVideoView = activityNonVideoView; 
  4.         this.activityVideoView = activityVideoView; 
  5.         this.loadingView = loadingView; 
  6.         this.webView = webView; 
  7.         this.isVideoFullscreen = false
  8.     } 
  9.   

這個(gè)構(gòu)造函數(shù)中的參數(shù),***個(gè)是webView的父布局,activityVideoView是另外的一個(gè)占滿整個(gè)屏幕的布局,loadingView是播放器的那個(gè)顯示緩沖狀態(tài)的view,webView就是webView啦!

 

見(jiàn)activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     xmlns:tools="http://schemas.android.com/tools" 
  4.     android:layout_width="match_parent" 
  5.     android:layout_height="match_parent" 
  6.     tools:context=".MainActivity" > 
  7.     <RelativeLayout 
  8.         android:id="@+id/nonVideoLayout" 
  9.         android:layout_width="match_parent" 
  10.         android:layout_height="match_parent" > 
  11.         <com.danielme.android.webviewdemo.VideoEnabledWebView 
  12.             android:id="@+id/webView" 
  13.             android:layout_width="match_parent" 
  14.             android:layout_height="match_parent" /> 
  15.     </RelativeLayout> 
  16.     <FrameLayout 
  17.         android:id="@+id/videoLayout" 
  18.         android:layout_width="match_parent" 
  19.         android:layout_height="match_parent" > 
  20.     </FrameLayout> 
  21. </RelativeLayout> 
  22.   

不多說(shuō)了,直接貼代碼VideoEnabledWebChromeClient.java代碼。

  1. package com.danielme.android.webviewdemo; 
  2.  
  3. import android.app.ActionBar.LayoutParams; 
  4. import android.media.MediaPlayer; 
  5. import android.media.MediaPlayer.OnCompletionListener; 
  6. import android.media.MediaPlayer.OnErrorListener; 
  7. import android.media.MediaPlayer.OnPreparedListener; 
  8. import android.view.View; 
  9. import android.view.ViewGroup; 
  10. import android.webkit.WebChromeClient; 
  11. import android.widget.FrameLayout; 
  12. import android.widget.VideoView; 
  13.  
  14. public class VideoEnabledWebChromeClient extends WebChromeClient implements OnPreparedListener, OnCompletionListener, OnErrorListener 
  15.     public interface ToggledFullscreenCallback 
  16.     { 
  17.         public void toggledFullscreen(boolean fullscreen); 
  18.     } 
  19.     private View activityNonVideoView; 
  20.     private ViewGroup activityVideoView; 
  21.     private View loadingView; 
  22.     private VideoEnabledWebView webView; 
  23.     private boolean isVideoFullscreen; // Indicates if the video is being displayed using a custom view (typically full-screen) 
  24.     private FrameLayout videoViewContainer; 
  25.     private CustomViewCallback videoViewCallback; 
  26.     private ToggledFullscreenCallback toggledFullscreenCallback; 
  27.     /** 
  28.      * Never use this constructor alone. 
  29.      * This constructor allows this class to be defined as an inline inner class in which the user can override methods 
  30.      */ 
  31.     public VideoEnabledWebChromeClient() 
  32.     { 
  33.     } 
  34.     /** 
  35.      * Builds a video enabled WebChromeClient. 
  36.      * @param activityNonVideoView A View in the activity's layout that contains every other view that should be hidden when the video goes full-screen. 
  37.      * @param activityVideoView A ViewGroup in the activity's layout that will display the video. Typically you would like this to fill the whole layout. 
  38.      */ 
  39.     public VideoEnabledWebChromeClient(View activityNonVideoView, ViewGroup activityVideoView) 
  40.     { 
  41.         this.activityNonVideoView = activityNonVideoView; 
  42.         this.activityVideoView = activityVideoView; 
  43.         this.loadingView = null
  44.         this.webView = null
  45.         this.isVideoFullscreen = false
  46.     } 
  47.     /** 
  48.      * Builds a video enabled WebChromeClient. 
  49.      * @param activityNonVideoView A View in the activity's layout that contains every other view that should be hidden when the video goes full-screen. 
  50.      * @param activityVideoView A ViewGroup in the activity's layout that will display the video. Typically you would like this to fill the whole layout. 
  51.      * @param loadingView A View to be shown while the video is loading (typically only used in API level <11). Must be already inflated and without a parent view. 
  52.      */ 
  53.     public VideoEnabledWebChromeClient(View activityNonVideoView, ViewGroup activityVideoView, View loadingView) 
  54.     { 
  55.         this.activityNonVideoView = activityNonVideoView; 
  56.         this.activityVideoView = activityVideoView; 
  57.         this.loadingView = loadingView; 
  58.         this.webView = null
  59.         this.isVideoFullscreen = false
  60.     } 
  61.     /** 
  62.      * Builds a video enabled WebChromeClient. 
  63.      * @param activityNonVideoView A View in the activity's layout that contains every other view that should be hidden when the video goes full-screen. 
  64.      * @param activityVideoView A ViewGroup in the activity's layout that will display the video. Typically you would like this to fill the whole layout. 
  65.      * @param loadingView A View to be shown while the video is loading (typically only used in API level <11). Must be already inflated and without a parent view. 
  66.      * @param webView The owner VideoEnabledWebView. Passing it will enable the VideoEnabledWebChromeClient to detect the HTML5 video ended event and exit full-screen. 
  67.      * Note: The web page must only contain one video tag in order for the HTML5 video ended event to work. This could be improved if needed (see Javascript code). 
  68.      */ 
  69.     public VideoEnabledWebChromeClient(View activityNonVideoView, ViewGroup activityVideoView, View loadingView, VideoEnabledWebView webView) 
  70.     { 
  71.         this.activityNonVideoView = activityNonVideoView; 
  72.         this.activityVideoView = activityVideoView; 
  73.         this.loadingView = loadingView; 
  74.         this.webView = webView; 
  75.         this.isVideoFullscreen = false
  76.     } 
  77.     /** 
  78.      * Indicates if the video is being displayed using a custom view (typically full-screen) 
  79.      * @return true it the video is being displayed using a custom view (typically full-screen) 
  80.      */ 
  81.     public boolean isVideoFullscreen() 
  82.     { 
  83.         return isVideoFullscreen; 
  84.     } 
  85.     /** 
  86.      * Set a callback that will be fired when the video starts or finishes displaying using a custom view (typically full-screen) 
  87.      * @param callback A VideoEnabledWebChromeClient.ToggledFullscreenCallback callback 
  88.      */ 
  89.     public void setOnToggledFullscreen(ToggledFullscreenCallback callback) 
  90.     { 
  91.         this.toggledFullscreenCallback = callback; 
  92.     } 
  93.     @Override 
  94.     public void onShowCustomView(View view, CustomViewCallback callback) 
  95.     { 
  96.         if (view instanceof FrameLayout) 
  97.         { 
  98.             // A video wants to be shown 
  99.             FrameLayout frameLayout = (FrameLayout) view; 
  100.             View focusedChild = frameLayout.getFocusedChild(); 
  101.             // Save video related variables 
  102.             this.isVideoFullscreen = true
  103.             this.videoViewContainer = frameLayout; 
  104.             this.videoViewCallback = callback; 
  105.             // Hide the non-video view, add the video view, and show it 
  106.             activityNonVideoView.setVisibility(View.GONE);             
  107.             activityVideoView.addView(videoViewContainer, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
  108.             activityVideoView.setVisibility(View.VISIBLE); 
  109.             if (focusedChild instanceof VideoView) 
  110.             { 
  111.                 // VideoView (typically API level <11) 
  112.                 VideoView videoView = (VideoView) focusedChild; 
  113.                 // Handle all the required events 
  114.                 videoView.setOnPreparedListener(this); 
  115.                 videoView.setOnCompletionListener(this); 
  116.                 videoView.setOnErrorListener(this); 
  117.             } 
  118.             else // Usually android.webkit.HTML5VideoFullScreen$VideoSurfaceView, sometimes android.webkit.HTML5VideoFullScreen$VideoTextureView 
  119.             { 
  120.                 // HTML5VideoFullScreen (typically API level 11+) 
  121.                 // Handle HTML5 video ended event 
  122.                 if (webView != null && webView.getSettings().getJavaScriptEnabled()) 
  123.                 { 
  124.                     // Run javascript code that detects the video end and notifies the interface 
  125.                     String js = "javascript:"
  126.                     js += "_ytrp_html5_video = document.getElementsByTagName('video')[0];"
  127.                     js += "if (_ytrp_html5_video !== undefined) {"
  128.                     { 
  129.                         js += "function _ytrp_html5_video_ended() {"
  130.                         { 
  131.                             js += "_ytrp_html5_video.removeEventListener('ended', _ytrp_html5_video_ended);"
  132.                             js += "_VideoEnabledWebView.notifyVideoEnd();"// Must match Javascript interface name and method of VideoEnableWebView 
  133.                         } 
  134.                         js += "}"
  135.                         js += "_ytrp_html5_video.addEventListener('ended', _ytrp_html5_video_ended);"
  136.                     } 
  137.                     js += "}"
  138.                     webView.loadUrl(js); 
  139.                 } 
  140.             } 
  141.  
  142.             // Notify full-screen change 
  143.             if (toggledFullscreenCallback != null
  144.             { 
  145.                 toggledFullscreenCallback.toggledFullscreen(true); 
  146.             } 
  147.         } 
  148.     } 
  149.     @Override 
  150.     public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) // Only available in API level 14+ 
  151.     { 
  152.         onShowCustomView(view, callback); 
  153.     } 
  154.     @Override 
  155.     public void onHideCustomView() 
  156.     { 
  157.         // This method must be manually (internally) called on video end in the case of VideoView (typically API level <11) 
  158.         // This method must be manually (internally) called on video end in the case of HTML5VideoFullScreen (typically API level 11+) because it's not always called automatically 
  159.         // This method must be manually (internally) called on back key press (from this class' onBackPressed() method) 
  160.         if (isVideoFullscreen) 
  161.         { 
  162.             // Hide the video view, remove it, and show the non-video view 
  163.             activityVideoView.setVisibility(View.GONE);//播放視頻的 
  164.             activityVideoView.removeView(videoViewContainer); 
  165.             activityNonVideoView.setVisibility(View.VISIBLE); 
  166.  
  167.             // Call back 
  168.             if (videoViewCallback != null) videoViewCallback.onCustomViewHidden(); 
  169.  
  170.             // Reset video related variables 
  171.             isVideoFullscreen = false
  172.             videoViewContainer = null
  173.             videoViewCallback = null
  174.  
  175.             // Notify full-screen change 
  176.             if (toggledFullscreenCallback != null
  177.             { 
  178.                 toggledFullscreenCallback.toggledFullscreen(false); 
  179.             } 
  180.         } 
  181.     } 
  182.     @Override 
  183.     public View getVideoLoadingProgressView() // Video will start loading, only called in the case of VideoView (typically API level <11) 
  184.     { 
  185.         if (loadingView != null
  186.         { 
  187.             loadingView.setVisibility(View.VISIBLE); 
  188.             return loadingView; 
  189.         } 
  190.         else 
  191.         { 
  192.             return super.getVideoLoadingProgressView(); 
  193.         } 
  194.     } 
  195.     @Override 
  196.     public void onPrepared(MediaPlayer mp) // Video will start playing, only called in the case of VideoView (typically API level <11) 
  197.     { 
  198.         if (loadingView != null
  199.         { 
  200.             loadingView.setVisibility(View.GONE); 
  201.         } 
  202.     } 
  203.  
  204.     @Override 
  205.     public void onCompletion(MediaPlayer mp) // Video finished playing, only called in the case of VideoView (typically API level <11) 
  206.     { 
  207.         onHideCustomView(); 
  208.     } 
  209.     @Override 
  210.     public boolean onError(MediaPlayer mp, int what, int extra) // Error while playing video, only called in the case of VideoView (typically API level <11) 
  211.     { 
  212.         return false// By returning false, onCompletion() will be called 
  213.     } 
  214.     /** 
  215.      * Notifies the class that the back key has been pressed by the user. 
  216.      * This must be called from the Activity's onBackPressed(), and if it returns false, the activity itself should handle it. Otherwise don't do anything. 
  217.      * @return Returns true if the event was handled, and false if it is not (video view is not visible) 
  218.      */ 
  219.     public boolean onBackPressed() 
  220.     { 
  221.         if (isVideoFullscreen) 
  222.         { 
  223.             onHideCustomView(); 
  224.             return true
  225.         } 
  226.         else 
  227.         { 
  228.             return false
  229.         } 
  230.     }     
  231.   

主要是onShowCustomView方法中,當(dāng)這個(gè)方法被調(diào)用,將含有webView的那個(gè)父布局隱藏掉(GONE),然后將***個(gè)參數(shù)view加到布局中。獲取***個(gè)參數(shù)view的子控件childView,進(jìn)行判斷childView是否屬于VideoView(Android 4.0之前是VideoView),如果是Android 4.0之后,則會(huì)執(zhí)行else中的代碼,新建String類型js代碼,然后調(diào)用loadUrl(js)就可以進(jìn)行視頻播放了。其中我個(gè)人不知道它是如何通過(guò)js來(lái)播放視頻的,我覺(jué)得和之前的addJavascriptInterface這個(gè)方法有一定關(guān)系,希望知道如何實(shí)現(xiàn)的能夠指導(dǎo)一下本人。其它的函數(shù)就很好理解了。

其中多說(shuō)一句,Android 4.0之前的那個(gè)***個(gè)參數(shù)view是videoView,Android 4.0之后是那個(gè)HTML5VideoFullScreen$VideoSurfaceView

責(zé)任編輯:張葉青 來(lái)源: eoe Android開(kāi)發(fā)者社區(qū)
相關(guān)推薦

2017-05-10 07:33:41

AndroidWebView視頻

2013-07-15 14:46:34

iOS開(kāi)發(fā)UIScrollVie移動(dòng)開(kāi)發(fā)

2023-03-29 09:37:49

視頻播放器應(yīng)用鴻蒙

2010-01-13 08:46:21

Windows 7USB自動(dòng)播放

2009-06-03 16:41:21

Eclipse亂碼Eclipse

2011-03-04 13:07:47

Filezilla

2010-04-16 12:42:44

無(wú)線路由器故障

2011-01-19 17:54:48

2009-05-31 09:07:35

Oracle鎖定

2015-11-30 12:02:25

DNS服務(wù)器

2015-03-23 11:16:38

網(wǎng)頁(yè)打不開(kāi)

2011-06-17 11:10:51

Qt 中文 輸出

2009-12-07 18:38:16

WCF異常

2011-03-30 11:04:11

mrtg

2010-01-15 09:38:08

磁盤(pán)被寫(xiě)保護(hù)解決辦法

2017-05-04 20:15:51

iOSNSTimer循環(huán)引用

2009-02-18 09:30:10

AJAX跨域XML

2020-05-09 10:59:33

git cloneLinux文件

2009-12-03 17:36:02

PHP Date()出

2009-11-30 10:55:16

PHP頁(yè)面亂碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)