我有一个用 ABS 和 Fragments 实现的选项卡式应用程序。其中一个片段应该包含一个显示某个 youtube 频道的 web 视图,用户可以在其中查看频道的视频并观看它们,如果他喜欢的话。因此,我想在 webview 中显示视频。一切正常,直到我点击播放观看视频。比它只显示加载圈。我尝试了一些在这里找到的方法,比如打开硬件加速或实现我自己的 WebChromeClient。要么它什么都没有改变,要么应用程序让我选择本机浏览器或 Chrome 来打开链接。知道我必须做什么才能让视频在 webview 中播放吗?附上我目前的实现:
private WebView wv;
private FrameLayout mContentView;
private FrameLayout mCustomViewContainer;
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
setHasOptionsMenu(true);
View v = inflater.inflate(R.layout.didacta_channel_layout, container, false);
wv = (WebView) v.findViewById(R.id.test_web);
mContentView = (FrameLayout) v.findViewById(R.id.main_content);
mCustomViewContainer = (FrameLayout) v.findViewById(R.id.fullscreen_custom_content);
wv.getSettings().setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT < 8) {
wv.getSettings().setPluginsEnabled(true);
} else {
wv.getSettings().setPluginState(PluginState.ON);
}
wv.setWebChromeClient(new DDChromeClient());
wv.loadUrl("http://www.youtube.com/user/bikltv");
return v;
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.add(0, 1, 0, "Restart")
.setIcon(R.drawable.ic_menu_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId() == android.R.id.home)
{
((DidactaNavigationActivity)getActivity()).finish();
}
if(item.getItemId() == 1)
{
wv.loadUrl("http://www.youtube.com/user/bikltv");
}
return super.onOptionsItemSelected(item);
}
class DDChromeClient extends WebChromeClient
{
public 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();
}
public 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);
}
}