Các mã sau đây là cần thiết để hiển thị các máy nghe nhạc video được bắt đầu bởi khung cốt lõi của web. Chìa khóa cho toàn bộ luồng là VideoView được chuyển lại cho WebChromeClient và bạn cần đính kèm chế độ xem đó vào hệ thống phân cấp chế độ xem của bạn.
Tôi đã tập hợp nó thông qua việc xem xét mã nguồn trình duyệt có sẵn như là một phần của AOSP.
Mã này tham chiếu 4 chế độ xem có thể không rõ ràng.Các hệ thống phân cấp view là:
FrameLayout mContentView
(gốc)
WebView mWebView
(con của mContentView)
FrameLAyout mCustomViewContainer
(con của mContentView)
View mCustomView
(con của mCustomViewContainer)
Quan điểm được định cấu hình như một phần của việc thiết lập hoạt động vùng chứa.
<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>
Trong hoạt động của mình onCreate
:
mContentView = (FrameLayout) findViewById(R.id.main_content);
mWebView = (WebView) findViewById(R.id.webView);
mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);
Đăng ký một WebChromeClient
với mWebView
. khách hàng mà nên ghi đè lên 2 sau - 4 phương pháp:
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;
}
Bạn cũng có thể muốn thêm hoạt động bindings vòng đời như:
@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();
}
}
Để hoạt động của bạn để làm cho hide video khi hoạt động là dừng lại hoặc nhấn nút quay lại.
Nguồn
2012-10-12 01:00:29
Điều này xảy ra với tất cả các phiên bản của Android cho bạn? Tôi đã nhìn thấy tương tự nhưng chỉ trên 4.1 – Nick
@Nick: Vấn đề này cũng với Android 3.0, 4.0. –
@RanjitChandel tôi có cùng một sự cố ... –