我正在尝试在我的应用程序的 web 视图中以全屏模式播放 Youtube 视频。我能够在 webview 中启用全屏按钮。
/** MainActivity
* Fullscreen video variables
*/
//Stores the custom view passed back by the WebChromeClient
private View mCustomView;
//Stores the main content view of the activity
private View mContentView;
//Container in which to place the custom view.
private FrameLayout mCustomViewContainer;
//Stores the CustomViewCallback interface.
private WebChromeClient.CustomViewCallback mCustomViewCallback;
/**
* Method to mirror onShowCustomView from the WebChrome client, allowing WebViews in a Fragment
* to show custom views.
* @param view - The custom view.
* @param callback - The callback interface for the custom view.
*/
public void showCustomView(View view, WebChromeClient.CustomViewCallback callback) {
//If there's already a custom view, this is a duplicate call, and we should
// terminate the new view, then bail out.
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
//Create a reusable set of FrameLayout.LayoutParams
FrameLayout.LayoutParams fullscreenParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
//Save the drawer view into an instance variable, then hide it.
mContentView = findViewById(R.id.activity_main);
mContentView.setVisibility(View.GONE);
//Create a new custom view container
mCustomViewContainer = new FrameLayout(InAppLandingPageActivity.this);
mCustomViewContainer.setLayoutParams(fullscreenParams);
mCustomViewContainer.setBackgroundResource(android.R.color.black);
//Set view to instance variable, then add to container.
mCustomView = view;
view.setLayoutParams(fullscreenParams);
mCustomViewContainer.addView(mCustomView);
mCustomViewContainer.setVisibility(View.VISIBLE);
//Save the callback an instance variable.
mCustomViewCallback = callback;
}
/**
* Method to mirror onShowCustomView from the WebChrome client, allowing WebViews in a Fragment
* to hide custom views.
*/
public void hideCustomView() {
if (mCustomView == null) {
//Nothing to hide - return.
return;
} else {
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
mCustomViewContainer.removeView(mCustomView);
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
mCustomView = null;
// Show the ActionBar
getSupportActionBar().show();
// Show the content view.
mContentView.setVisibility(View.VISIBLE);
}
}
铬客户端:
private class MyWebChromeClient extends ChromeClient {
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
MainActivity activity = (MainActivity) getActivity();
try {
activity.showCustomView(view, callback);
} catch (Exception ex) {
Logger.i(TAG, "Failed onShowCustomView", ex);
}
}
@Override
public void onHideCustomView() {
super.onHideCustomView();
MainActivity activity = (MainActivity) getActivity();
activity.hideCustomView();
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:pandora="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/web_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
当我单击全屏按钮时,我收到以下错误,灰屏并且视频音频仍在播放 [ERROR:shared_renderer_state.cc] 无法请求 GL 进程。可能出现死锁