The following code is required to display the video player launched by the web core framework. The key to the whole stream is that the VideoView is passed back to WebChromeClient, and you need to attach this view to your view hierarchy.
I compiled it by looking at the source code of the browser, available as part of AOSP.
This code refers to 4 species that may not be obvious. View Hierarchy:
FrameLayout mContentView (root)WebView mWebView (child of mContentView)FrameLAyout mCustomViewContainer (child of mContentView)View mCustomView (child of mCustomViewContainer)
Views are configured as part of the container activity setting.
<FrameLayout android:id="@+id/fullscreen_custom_content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF000000"/> <FrameLayout android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > <WebView android:id="@+id/webView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </FrameLayout> </FrameLayout>
In your onCreate actions:
mContentView = (FrameLayout) findViewById(R.id.main_content); mWebView = (WebView) findViewById(R.id.webView); mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);
Register a WebChromeClient with mWebView . This client should override the following 2 to 4 methods:
void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) { // if a view already exists then immediately terminate the new one if (mCustomView != null) { callback.onCustomViewHidden(); return; } // Add the custom view to its container. mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER); mCustomView = view; mCustomViewCallback = callback; // hide main browser view mContentView.setVisibility(View.GONE); // Finally show the custom view container. mCustomViewContainer.setVisibility(View.VISIBLE); mCustomViewContainer.bringToFront(); } void onHideCustomView() { 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(); // Show the content view. mContentView.setVisibility(View.VISIBLE); } public Bitmap getDefaultVideoPoster() { if (mDefaultVideoPoster == null) { mDefaultVideoPoster = BitmapFactory.decodeResource(getResources(), R.drawable.default_video_poster); } return mDefaultVideoPoster; } public View getVideoLoadingProgressView() { if (mVideoProgressView == null) { LayoutInflater inflater = LayoutInflater.from(this); mVideoProgressView = inflater.inflate(R.layout.video_loading_progress, null); } return mVideoProgressView; }
You can also add activity lifecycle bindings, for example:
@Override protected void onStop() { super.onStop(); if (mCustomView != null) { if (mCustomViewCallback != null) mCustomViewCallback.onCustomViewHidden(); mCustomView = null; } } @Override public void onBackPressed() { if (mCustomView != null) { onHideCustomView(); } else { finish(); } }
In its activity, to make the video hidden when the action is stopped or the back button is pressed.
Nick Campion Oct 12 '12 at 1:00 2012-10-12 01:00
source share