这是我的代码。我为youtube构建了webview,并使用WebChromClient试图在应用程序上添加全屏功能。但是当点击全屏时,它会显示空白屏幕,而auido则会在背景中播放。
MainAvtivity
public class MainActivity extends AppCompatActivity {
public WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview = (WebView)findViewById(R.id.youtube_webview);
WebSettings webSettings =mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.setWebChromeClient(new CrmClient());
mywebview.setWebViewClient(new WebViewClient());
mywebview.getSettings().setJavaScriptEnabled(true);
mywebview.setVerticalScrollBarEnabled(false);
mywebview.setHorizontalScrollBarEnabled(false);
mywebview.loadUrl("https://www.youtube.com");
}
class CrmClient extends WebChromeClient{
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
}
@Override
public void onHideCustomView() {
super.onHideCustomView();
}
}
@Override
protected void onPause() {
super.onPause();
if(mywebview != null) {
mywebview.onPause();
mywebview.pauseTimers();
}
}
@Override
protected void onResume() {
super.onResume();
if(mywebview != null){
mywebview.onResume();
mywebview.resumeTimers();
}
}
@Override
public void onBackPressed() {
if (mywebview.canGoBack())
mywebview.goBack();
else
super.onBackPressed();
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/youtube_webview" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</FrameLayout>
</WebView>
AndroidMainFest.XML
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:label="@string/app_name">
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:0)
尝试此代码
public class MainActivity extends AppCompatActivity {
public WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview = (WebView)findViewById(R.id.youtube_webview);
WebSettings webSettings =mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.setWebViewClient(new WebViewClient());
mywebview.getSettings().setJavaScriptEnabled(true);
mywebview.setVerticalScrollBarEnabled(false);
mywebview.setHorizontalScrollBarEnabled(false);
mywebview.loadUrl("https://www.youtube.com");
mywebview.setWebChromeClient(new WebChromeClient() {
public void onShowCustomView(View view,
WebChromeClient.CustomViewCallback callback) {
if (mCustomView != null) {
onHideCustomView();
return;
}
mCustomView = view;
mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
mOriginalOrientation = getRequestedOrientation();
mCustomViewCallback = callback;
FrameLayout decor = (FrameLayout) getWindow().getDecorView();
decor.addView(mCustomView, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
@Override
public void onHideCustomView() {
// 1. Remove the custom view
FrameLayout decor = (FrameLayout) getWindow().getDecorView();
decor.removeView(mCustomView);
mCustomView = null;
// 2. Restore the state to it's original form
getWindow().getDecorView()
.setSystemUiVisibility(mOriginalSystemUiVisibility);
setRequestedOrientation(mOriginalOrientation);
mCustomViewCallback.onCustomViewHidden();
mCustomViewCallback = null;
}
});
}
并添加
private View mCustomView;
private int mOriginalSystemUiVisibility;
private int mOriginalOrientation;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
protected FrameLayout mFullscreenContainer;
答案 1 :(得分:0)
尝试此代码
private class MyChrome extends WebChromeClient {
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
protected FrameLayout mFullscreenContainer;
private int mOriginalOrientation;
private int mOriginalSystemUiVisibility;
MyChrome() {}
public Bitmap getDefaultVideoPoster()
{
if (mCustomView == null) {
return null;
}
return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
}
public void onHideCustomView()
{
((FrameLayout)getWindow().getDecorView()).removeView(this.mCustomView);
this.mCustomView = null;
getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
setRequestedOrientation(this.mOriginalOrientation);
this.mCustomViewCallback.onCustomViewHidden();
this.mCustomViewCallback = null;
}
public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
{
if (this.mCustomView != null)
{
onHideCustomView();
return;
}
this.mCustomView = paramView;
this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
this.mOriginalOrientation = getRequestedOrientation();
this.mCustomViewCallback = paramCustomViewCallback;
((FrameLayout)getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
getWindow().getDecorView().setSystemUiVisibility(3846);
}
}
并添加此代码
myWebView.setWebChromeClient(new MyChrome());