In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Android 4.0 or above WebView can not play video full screen solution, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Last time I made a simple browser using webView! There are two problems, one is to host the content that needs to be downloaded in the browser to the system's default download program for download, which is relatively simple and will not be discussed here; the other problem is that our Android device version is 4.0.3, which can not support full-screen video playback like Android2.3, this problem is more tangled, but after continuous exploration, we finally solved this problem. I'd like to share with you the solution:
1. First define a VideoEnabledWebView that inherits from WebView, and copy the loadData,loadDataWithBaseURL,loadUrl method in it. The simple reason is to initialize something when url or js is loaded. See the code:
Package com.danielme.android.webviewdemo; import java.util.Map; import android.annotation.SuppressLint; import android.content.Context; import android.os.Handler; import android.os.Looper; import android.util.AttributeSet; import android.webkit.WebChromeClient; import android.webkit.WebView; public class VideoEnabledWebView extends WebView {public interface ToggledFullscreenCallback {public void toggledFullscreen (boolean fullscreen);} private VideoEnabledWebChromeClient videoEnabledWebChromeClient; private boolean addedJavascriptInterface Public VideoEnabledWebView (Context context) {super (context); addedJavascriptInterface = false;} public VideoEnabledWebView (Context context, AttributeSet attrs) {super (context, attrs); addedJavascriptInterface = false;} public VideoEnabledWebView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); addedJavascriptInterface = false } / * Pass only a VideoEnabledWebChromeClient instance. * / @ Override @ SuppressLint ("SetJavaScriptEnabled") public void setWebChromeClient (WebChromeClient client) {getSettings () .setJavaScriptEnabled (true); if (client instanceof VideoEnabledWebChromeClient) {this.videoEnabledWebChromeClient = (VideoEnabledWebChromeClient) client;} super.setWebChromeClient (client);} @ Override public void loadData (String data, String mimeType, String encoding) {addJavascriptInterface () Super.loadData (data, mimeType, encoding);} @ Override public void loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl) {addJavascriptInterface (); super.loadDataWithBaseURL (baseUrl, data, mimeType, encoding, historyUrl) } @ Override public void loadUrl (String url) {addJavascriptInterface (); super.loadUrl (url);} @ Override public void loadUrl (String url, Map additionalHttpHeaders) {addJavascriptInterface (); super.loadUrl (url, additionalHttpHeaders);} private void addJavascriptInterface () {System.out.println (addedJavascriptInterface) If (! addedJavascriptInterface) {/ / Add javascript interface to be called when the video ends (must be done before page load) addJavascriptInterface (new Object () {}, "_ VideoEnabledWebView"); / / Must match Javascript interface name of VideoEnabledWebChromeClient addedJavascriptInterface = true;}}
Where the addJavascriptInterface method binds a current java object to a javascript, using the following method
Webv.addJavascriptInterface (this, "_ VideoEnabledWebView"); / / this is the current object and is bound to _ VideoEnabledWebView of js. The scope of the main _ VideoEnabledWebView is global. I do not quite understand the content of this part, provide links for you to learn, hope that friends who understand can teach what this step is for! (http://www.oschina.net/code/snippet_232612_8531)
2. Define a class VideoEnabledWebChromeClient that inherits from WebChromeClient. The onShowCustomView method in this WebChromeClient is the method that will be called when the network video is played, and the onHideCustomView method will be called when the video playback is completed. One of the constructors needs to be mentioned:
Public VideoEnabledWebChromeClient (View activityNonVideoView, ViewGroup activityVideoView, View loadingView, VideoEnabledWebView webView) {this.activityNonVideoView = activityNonVideoView; this.activityVideoView = activityVideoView; this.loadingView = loadingView; this.webView = webView; this.isVideoFullscreen = false;}
The parameters in this constructor, * are the parent layout of webView, activityVideoView is another layout that occupies the entire screen, and loadingView is the view,webView that displays the buffered state of the player is webView!
See activity_main.xml
Say no more, just paste the code VideoEnabledWebChromeClient.java code.
Package com.danielme.android.webviewdemo; import android.app.ActionBar.LayoutParams; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnErrorListener; import android.media.MediaPlayer.OnPreparedListener; import android.view.View; import android.view.ViewGroup; import android.webkit.WebChromeClient; import android.widget.FrameLayout; import android.widget.VideoView; public class VideoEnabledWebChromeClient extends WebChromeClient implements OnPreparedListener, OnCompletionListener, OnErrorListener {public interface ToggledFullscreenCallback {public void toggledFullscreen (boolean fullscreen);} private View activityNonVideoView Private ViewGroup activityVideoView; private View loadingView; private VideoEnabledWebView webView; private boolean isVideoFullscreen; / / Indicates if the video is being displayed using a custom view (typically full-screen) private FrameLayout videoViewContainer; private CustomViewCallback videoViewCallback; private ToggledFullscreenCallback toggledFullscreenCallback; / * Never use this constructor alone. * This constructor allows this class to be defined as an inline inner class in which the user can override methods * / public VideoEnabledWebChromeClient () {} / * * Builds a video enabled WebChromeClient. * @ param activityNonVideoView A View in the activity's layout that contains every other view that should be hidden when the video goes full-screen. * @ param activityVideoView A ViewGroup in the activity's layout that will display the video. Typically you would like this to fill the whole layout. * / public VideoEnabledWebChromeClient (View activityNonVideoView, ViewGroup activityVideoView) {this.activityNonVideoView = activityNonVideoView; this.activityVideoView = activityVideoView; this.loadingView = null; this.webView = null; this.isVideoFullscreen = false;} / * Builds a video enabled WebChromeClient. * @ param activityNonVideoView A View in the activity's layout that contains every other view that should be hidden when the video goes full-screen. * @ param activityVideoView A ViewGroup in the activity's layout that will display the video. Typically you would like this to fill the whole layout. * @ param loadingView A View to be shown while the video is loading (typically only used in API level
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.