gpt4 book ai didi

android - 关闭从 WebView 打开的全屏视频后出现白屏

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:16:03 25 4
gpt4 key购买 nike

我有一个嵌入了 youtube 视频的 WebView。我已经使用像这样的简单对话框解决方案实现了全屏模式:

webView.setWebChromeClient(new CustomWebChromeClient());

public class CustomWebChromeClient extends WebChromeClient {
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
Dialog dialog = new Dialog(ArticleDetailsActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
view.setBackgroundColor(getResources().getColor(R.color.black));
dialog.setContentView(view);
dialog.show();
}

@Override
public void onHideCustomView() {
super.onHideCustomView();
}
}

除了使用后退按钮关闭视频的那一刻外,它工作得很好,因为然后全屏视频消失并且白色全屏覆盖层停留在屏幕上,直到另一个后退按钮使用。我试图变得聪明,并在 onHideCustomView() 中执行了 onBackPressed(),但随后整个 Activity 都完成了。

如何摆脱那个白色的窗帘?

最佳答案

深入研究了 android 代码并找到了解决方案:

    public class CustomWebChromeClient extends WebChromeClient {

@Override
public void onShowCustomView(View view, final CustomViewCallback callback) {
Dialog dialog = new Dialog(ArticleDetailsActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
view.setBackgroundColor(getResources().getColor(R.color.black));
dialog.setContentView(view);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
callback.onCustomViewHidden();
chromeWebClient.onHideCustomView();
}
});
dialog.show();
}

@Override
public void onHideCustomView() {
super.onHideCustomView();
}
}

相关问题 - 已解决:原来当重新打开 Activity 并再次播放视频时 HTML5VideoView.reprepareData 或调用 webView.onPause()HTML5VideoView.isPlaying 有时会崩溃这似乎是另一个问题......

最后的说明

为了 WebView 正常工作并且不泄漏内存,您应该在 ActivityFragment 生命周期回调中调用相应的 WebViewMethods,如下所示 Activity(可能与 Fragment 有点相似):

@Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
webView.restoreState(savedInstanceState);
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(BundleKeys.HAS_PHOTOS, hasPhotos);
outState.putLong(BundleKeys.ARTICLE_ID, articleId);
webView.saveState(outState);
}

@Override
protected void onResume() {
webView.onResume();
}

@Override
protected void onPause() {
super.onPause();
webView.onPause();
}

@Override
protected void onStop() {
super.onStop();
webView.stopLoading();
}

@Override
protected void onDestroy() {
super.onDestroy();
webView.destroy();
}

关于android - 关闭从 WebView 打开的全屏视频后出现白屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34718725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com