我正在使用HTML5WebView播放vimeo视频。首先,我在列表视图中显示视频列表,然后单击其中一个项目,打开使用HTML5WebView播放视频的活动。它适用于第一次播放,并为任何其他vimeo视频提供例外。例外情况如下:
11-17 10:35:14.989: E/AndroidRuntime(30576): FATAL EXCEPTION: main
11-17 10:35:14.989: E/AndroidRuntime(30576): java.lang.NullPointerException
11-17 10:35:14.989: E/AndroidRuntime(30576): at android.webkit.HTML5VideoView.setVolume(HTML5VideoView.java:219)
11-17 10:35:14.989: E/AndroidRuntime(30576): at android.webkit.HTML5VideoViewProxy$VideoPlayer.setVolume(HTML5VideoViewProxy.java:326)
11-17 10:35:14.989: E/AndroidRuntime(30576): at android.webkit.HTML5VideoViewProxy.handleMessage(HTML5VideoViewProxy.java:488)
11-17 10:35:14.989: E/AndroidRuntime(30576): at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 10:35:14.989: E/AndroidRuntime(30576): at android.os.Looper.loop(Looper.java:137)
11-17 10:35:14.989: E/AndroidRuntime(30576): at android.app.ActivityThread.main(ActivityThread.java:4950)
11-17 10:35:14.989: E/AndroidRuntime(30576): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 10:35:14.989: E/AndroidRuntime(30576): at java.lang.reflect.Method.invoke(Method.java:511)
11-17 10:35:14.989: E/AndroidRuntime(30576): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
11-17 10:35:14.989: E/AndroidRuntime(30576): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
11-17 10:35:14.989: E/AndroidRuntime(30576): at dalvik.system.NativeStart.main(Native Method)
HTML5WebView代码(我从其中一个github repos获得)如下:
public class Html5WebView extends WebView {
private Context mContext;
private MyWebChromeClient mWebChromeClient;
private View mCustomView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private FrameLayout mContentView;
private FrameLayout mBrowserFrameLayout;
private FrameLayout mLayout;
static final String LOGTAG = "HTML5WebView";
public Html5WebView(Context context) {
super(context);
init(context);
}
public Html5WebView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public Html5WebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
mContext = context;
Activity a = (Activity) mContext;
mLayout = new FrameLayout(context);
mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(a).inflate(
R.layout.custom_screen, null);
mContentView = (FrameLayout) mBrowserFrameLayout
.findViewById(R.id.main_content);
mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
.findViewById(R.id.fullscreen_custom_content);
mLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
// Configure the webview
WebSettings s = getSettings();
s.setBuiltInZoomControls(true);
s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
s.setUseWideViewPort(true);
s.setLoadWithOverviewMode(true);
// s.setSavePassword(true);
s.setSaveFormData(true);
s.setJavaScriptEnabled(true);
mWebChromeClient = new MyWebChromeClient();
setWebChromeClient(mWebChromeClient);
setWebViewClient(new WebViewClient());
setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
// enable navigator.geolocation
// s.setGeolocationEnabled(true);
// s.setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/");
// enable Web Storage: localStorage, sessionStorage
s.setDomStorageEnabled(false);
mContentView.addView(this);
}
public FrameLayout getLayout() {
return mLayout;
}
public boolean inCustomView() {
return (mCustomView != null);
}
public void hideCustomView() {
mWebChromeClient.onHideCustomView();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if ((mCustomView == null) && canGoBack()) {
goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
private class MyWebChromeClient extends WebChromeClient {
private Bitmap mDefaultVideoPoster;
private View mVideoProgressView;
@Override
public void onShowCustomView(View view,
WebChromeClient.CustomViewCallback callback) {
// Log.i(LOGTAG, "here in on ShowCustomView");
Html5WebView.this.setVisibility(View.GONE);
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mCustomViewContainer.addView(view);
mCustomView = view;
mCustomViewCallback = callback;
mCustomViewContainer.setVisibility(View.VISIBLE);
}
@Override
public void onHideCustomView() {
System.out.println("customview hideeeeeeeeeeeeeeeeeeeeeeeeeee");
if (mCustomView == null)
return;
// 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();
Html5WebView.this.setVisibility(View.VISIBLE);
Html5WebView.this.goBack();
// Log.i(LOGTAG, "set it to webVew");
}
@Override
public View getVideoLoadingProgressView() {
// Log.i(LOGTAG, "here in on getVideoLoadingPregressView");
if (mVideoProgressView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
mVideoProgressView = inflater.inflate(
R.layout.video_loading_progress, null);
}
return mVideoProgressView;
}
@Override
public void onReceivedTitle(WebView view, String title) {
((Activity) mContext).setTitle(title);
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
((Activity) mContext).getWindow().setFeatureInt(
Window.FEATURE_PROGRESS, newProgress * 100);
}
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
}
static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
}
在活动中,我只是拥有这么多代码: (该ID是来自带有视频列表的活动的vimeo视频ID。)
public static class PlaceholderFragment extends Fragment {
private Html5WebView mWebView;
private String url;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mWebView = new Html5WebView(getActivity());
url = "http://player.vimeo.com/video/"+id;
System.out.println("url::"+url);
mWebView.loadUrl(url);
return mWebView.getLayout();
}
}