我知道這是一個古老的問題,它被問了很多次。但我嘗試了很多解決方案,他們都不適合我。如何在全屏模式下播放視頻並退出Android WebView的全屏模式?
我在華爲6P和三星S7,Nexus 5, WebView中的視頻(https://www.youtube-nocookie.com/embed/M68lNfqmTNk?rel=0&rel=0&showinfo=0)沒有像預期的那樣工作(它在去年工作過)。 當我點擊全屏按鈕時,會繼續調用「onShowCustomView」和「onHideCustomView」。所以我可以看到視頻閃爍(非全屏 - >全屏 - >非全屏)。 有時候,如果我很幸運,視頻會進入全屏模式,但如果按全屏按鈕退出全屏模式,該按鈕根本不起作用。
我在GitHub寫了一個demo,希望有人能幫助我。謝謝。
下面是代碼
初始化網頁視圖:
mWebView = (WebView)findViewById(R.id.webview);
mWebChromeClient = new MyWebChromeClient();
mWebView.setWebChromeClient(mWebChromeClient);
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
mWebView.loadUrl(url);
return true;
}
});
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setAllowFileAccess(true);
webSettings.setSupportZoom(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
MyWebChromeClient:
public class MyWebChromeClient extends WebChromeClient {
FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
Log.d("youtube", "onShowCustomView");
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mContentView = (ConstraintLayout) findViewById(R.id.activity_main);
mContentView.setVisibility(View.GONE);
mCustomViewContainer = new FrameLayout(MainActivity.this);
mCustomViewContainer.setLayoutParams(LayoutParameters);
mCustomViewContainer.setBackgroundResource(android.R.color.black);
view.setLayoutParams(LayoutParameters);
mCustomViewContainer.addView(view);
mCustomView = view;
mCustomViewCallback = callback;
mCustomViewContainer.setVisibility(View.VISIBLE);
setContentView(mCustomViewContainer);
}
@Override
public void onHideCustomView() {
Log.d("youtube", "onHideCustomView");
if (mCustomView == null) {
return;
} else {
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
mCustomViewContainer.removeView(mCustomView);
mCustomView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
// Show the content view.
mContentView.setVisibility(View.VISIBLE);
setContentView(mContentView);
}
}
}
我沒設置安卓的硬件加速在AndroidManifest.xml
android:hardwareAccelerated="true"
https://stackoverflow.com/questions/38842869/how-to-fullscreen-youtube-video-in-webview請參考這個希望這個幫助! – Kushan2
謝謝。我試過它不適合我。 – User9527
https://stackoverflow.com/questions/15796661/android-webview-app-wont-let-video-player-go-full-screen/16199649#16199649你見過這個嗎? – Kushan2