问题
So I've created an android app that plays video from blip.tv and various other flash-enabled websites. I use a Webview
to display the video. Here is my code:
try{
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
view = new WebView(this);
view.getSettings().setPluginsEnabled(true);
view.getSettings().setPluginState(WebSettings.PluginState.ON);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setLoadWithOverviewMode(true);
view.getSettings().setUseWideViewPort(true);
view.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
view.setScrollbarFadingEnabled(true);
// get the html code
String html = getIntent().getStringExtra("VIDEO_CONTENT");
view.loadData(html, "text/html", null);
setContentView(view);
}
catch(Exception e){
}
The problem is with android 4 (Ice Cream Sandwich). When I try to put the app into full screen mode, the app crashes. The app does NOT crash on android 2.3 or 3.0 however. Any ideas to fix this?
回答1:
This happens in ICS because the show()
method in android.webkit.PluginFullScreenHolder
gets called when trying to go to fullscreen mode. This method does the following:
WebChromeClient client = mWebView.getWebChromeClient();
client.onShowCustomView(mLayout, mOrientation, mCallback);
If you do not set the WebChromeClient
on your WebView
, you will get an NPE.
This fixed our crash, however the WebView disappears and the fullscreen video is not shown.
See: Android ICS 4.0 Placing Flash WebView into full screen calls hideAll Method?
*** Update:
Ultimately, in order to get my WebView
to play flash videos in full screen mode I had to implement the onShowCustomView()
method in my WebChromeClient
in a fashion similar to what was done in the source code for the Android browser. The implementation of that method that I used for inspiration was in the BaseUI class:
https://github.com/android/platform_packages_apps_browser/blob/master/src/com/android/browser/BaseUi.java
I don't fully understand exactly what is happening here. I also wish I understood why the developers on ICS decided to require this method to be implemented. I wish I knew the value, or what problem this solved. In past versions, this fullscreen mode "just worked" now it requires a lot of digging.
回答2:
I was facing the same problem. I asked and got the following answer which works for me.
Hope this will help some people:
Create the FullscreenableChromeClient and
add this line:
WebView.setWebChromeClient( new FullscreenableChromeClient(this));
public class FullscreenableChromeClient extends WebChromeClient {
protected Activity mActivity = null;
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private int mOriginalOrientation;
private FrameLayout mContentView;
private FrameLayout mFullscreenContainer;
private static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
public FullscreenableChromeClient(Activity activity) {
this.mActivity = activity;
}
@Override
public void onShowCustomView(View view, int requestedOrientation, WebChromeClient.CustomViewCallback callback) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mOriginalOrientation = mActivity.getRequestedOrientation();
FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
mFullscreenContainer = new FullscreenHolder(mActivity);
mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
mCustomView = view;
setFullscreen(true);
mCustomViewCallback = callback;
mActivity.setRequestedOrientation(requestedOrientation);
}
super.onShowCustomView(view, requestedOrientation, callback);
}
@Override
public void onHideCustomView() {
if (mCustomView == null) {
return;
}
setFullscreen(false);
FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
decor.removeView(mFullscreenContainer);
mFullscreenContainer = null;
mCustomView = null;
mCustomViewCallback.onCustomViewHidden();
mActivity.setRequestedOrientation(mOriginalOrientation);
}
private void setFullscreen(boolean enabled) {
Window win = mActivity.getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
if (enabled) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
if (mCustomView != null) {
mCustomView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
} else {
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}
win.setAttributes(winParams);
}
private static class FullscreenHolder extends FrameLayout {
public FullscreenHolder(Context ctx) {
super(ctx);
setBackgroundColor(ctx.getResources().getColor(android.R.color.black));
}
@Override
public boolean onTouchEvent(MotionEvent evt) {
return true;
}
}
}
回答3:
Okay, so I think this is an android 4.0.2 / flash player problem, because other apps like browser
and dolphin mini
crash when flash is put into full screen mode.
来源:https://stackoverflow.com/questions/8660866/flash-player-crashes-when-trying-to-enter-fullscreen-mode-android-4-0