android WebView watch full-screen video in full screen Watch live

1. Change the manifest file

Activity in the statement you are placed in WebView add the following code:

android:hardwareAccelerated ="true"

android:configChanges="orientation|screenSize|keyboardHidden"

2. Java class change

1. WebView initialization settings:

@JavascriptInterface

private void initWebView() {

WebSettings mWebSettings = mWebView.getSettings();

mWebSettings.setSupportZoom(true);

mWebSettings.setLoadWithOverviewMode(true);

mWebSettings.setUseWideViewPort(true);

mWebSettings.setDefaultTextEncodingName("utf-8");

mWebSettings.setLoadsImagesAutomatically(true);

mWebSettings.setBlockNetworkImage (false); // solve the picture does not show

mWebSettings.setTextZoom (100); // set the default zoom level, follow the system to prevent the page change font size

@ {// Pending projects

mWebSettings.setAllowFileAccess(true);

mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);

mWebSettings.setDomStorageEnabled (true); // must be preserved, otherwise you can not play Youku video, the other OK

try {

if (Build.VERSION.SDK_INT >= 16) {

Class clazz = mWebSettings.getClass();

Method method = clazz.getMethod("setAllowUniversalAccessFromFileURLs", boolean.class);

if (method != null) {

method.invoke(mWebSettings, true);

}

}

} catch (Exception e) {

e.printStackTrace ();

}

mWebSettings.setPluginState(WebSettings.PluginState.ON);

CookieManager cookieManager = CookieManager.getInstance();

cookieManager.setAcceptCookie(true);

//@}

// call the JS method. Android version is greater than 17, add comments @JavascriptInterface

mWebSettings.setJavaScriptEnabled(true);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

mWebSettings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

}

mWebView.setWebChromeClient (webChromeClient);

}

Property is set to indicate the pending projects dispensable, does not affect the paper said functionality is implemented. Newell Agent Application

2. To achieve chrome Agent:

WebChromeClient webChromeClient = new WebChromeClient() {

WebChromeClient.CustomViewCallback mCallback;

@Override

public void onShowCustomView(View view, CustomViewCallback callback) {

fullScreen();

mCallback = callback;

super.onShowCustomView(view, callback);

}

@Override

public void onHideCustomView() {

fullScreen();

super.onHideCustomView();

}

};

private void fullScreen() {

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

LogUtil.i(TAG+" 横屏");

} else {

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

LogUtil.i(TAG+" 竖屏");

}

}

3. Rewrite onConfigurationChanged method:

@Override

public void onConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);

switch (newConfig.orientation) {

case Configuration.ORIENTATION_LANDSCAPE:

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

break;

case Configuration.ORIENTATION_PORTRAIT:

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

break;

}

}

OK, follow the above steps after pasting finished and you're done! You can copy a link to this page to open the Live platform, and then click the full screen to watch live test.

Guess you like

Origin blog.51cto.com/14511863/2477578