gpt4 book ai didi

java - WebView 中视频的全屏按钮不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:41:55 25 4
gpt4 key购买 nike

我正在尝试实现 WebChromeClient 的一个子类,以便在 WebView 内的 youtube 嵌入视频链接(例如:https://www.youtube.com/embed/dQw4w9WgXcQ)上有一个可用的全屏按钮。

我基本上简化了this repo ,这里有一些 fragment :

VideoWebChromeClient:

public class VideoWebChromeClient extends WebChromeClient {
private boolean isVideoFullscreen = false;
private View activityNonVideoView;
private ViewGroup activityVideoView;
private View videoViewContainer;
private CustomViewCallback videoCallback;
private Window videoWindow;

public VideoWebChromeClient(View activityNonVideoView, ViewGroup activityVideoView, Window window) {
this.activityNonVideoView = activityNonVideoView;
this.activityVideoView = activityVideoView;
this.videoWindow = window;
}

@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
Log.w("ENTER FULLSCREEN");
videoCallback = callback;
videoViewContainer = view;
videoWindow.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
activityNonVideoView.setVisibility(View.GONE);
activityVideoView.addView(videoViewContainer, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
activityVideoView.setVisibility(View.VISIBLE);
isVideoFullscreen = true;
}

@Override
public void onHideCustomView() {
if (!isVideoFullscreen) {
return;
}
Log.w("EXIT FULLSCREEN");
activityVideoView.setVisibility(View.GONE);
activityVideoView.removeView(videoViewContainer);
activityNonVideoView.setVisibility(View.VISIBLE);
videoWindow.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
videoViewContainer = null;
videoCallback.onCustomViewHidden();
isVideoFullscreen = false;
}

public boolean onBackPressed() {
onHideCustomView();
return isVideoFullscreen;
}
}

WebViewActivity

public class WebViewActivity extends BaseActivity {

public static final String WEB_VIEW_URL_EXTRA = "URL";
private boolean loadedFirstURL = true;
private VideoWebChromeClient mWebChromeClient;

@Bind(webview)
WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(provideLayoutId());
String url = (String) getIntent().getSerializableExtra(WEB_VIEW_URL_EXTRA);

// Check if URL is YouTube / Vimeo
if (RegexHelper.isVideoURL(url)){
// Allow Fullscreen
final View defaultLayout = findViewById(R.id.defaultLayout);
final View fullscreenLayout = findViewById(R.id.fullscreenLayout);
mWebChromeClient = new VideoWebChromeClient(defaultLayout, (ViewGroup) fullscreenLayout, getWindow());
mWebView.setWebChromeClient(mWebChromeClient);

// Block external links
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (loadedFirstURL) {
loadedFirstURL = false;
return false;
}
return true;
}
});
}
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
}

@Override
public void onBackPressed() {
if (!mWebChromeClient.onBackPressed())
{
if (mWebView.canGoBack()) {
mWebView.goBack();
}
else {
super.onBackPressed();
}
}
}
}

它在 KitKat 上正常工作,但在 Nougat 上不能正常工作(我只有这两个设备可以测试):当我点击全屏按钮时,它有时会进入全屏,但大多数时候它会“闪烁”并且保持非全屏状态。

然后,当它实际上处于全屏模式时,退出全屏的按钮不起作用(onHideCustomView 根本没有被调用)

您可以看到我放置了日志并注意到当“闪光”发生时,onHideCustomView 实际上是在 onShowCustomView 之后调用的("ENTER FULLSCREEN" 然后立即 "EXIT FULLSCREEN"), 我不明白为什么。

如果有人能指出我的解决方案,非常感谢

最佳答案

我也遇到了同样的问题,但我花了几个小时解决了它。

您所要做的就是设置 KitKat 设备的 UserAgent 字符串。就是这样!

private String userAgent = "Mozilla/5.0 (Linux; Android 4.4; Nexus 5 Build/_BuildID_) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36";
mWebView.getSettings().setUserAgentString(userAgent);

这将修复新设备上 onShowCustomView()onHideCustomView() 的所有奇怪功能。希望对您有所帮助。

关于java - WebView 中视频的全屏按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44207703/

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