android WebView全屏觀看視頻 全屏觀看直播

1.改清單文件

在你放置WebView的Activity的聲明中加上以下代碼:

android:hardwareAccelerated ="true"

android:configChanges="orientation|screenSize|keyboardHidden"

2.改Java類

1.對WebView進行初始化設置:

@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);//解決圖片不顯示

mWebSettings.setTextZoom(100);//設置默認縮放比例,防止網頁跟隨系統字體大小變化

//待定項目@{

mWebSettings.setAllowFileAccess(true);

mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);

mWebSettings.setDomStorageEnabled(true);// 必須保留,否則無法播放優酷視頻,其他的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);

//@}

//調用JS方法.安卓版本大於17,加上註解 @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);

}

標明待定項目的屬性設置可有可無,不影響本文說的功能實現。智匯代理申請

2.實現chrome代理:

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.重寫onConfigurationChanged方法:

@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,按照以上步驟粘貼完之後就大功告成了!你可以在直播平臺裏複製一個鏈接到該頁面去打開,然後點擊全屏觀看直播測試一下。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章