Android:频道的Youtube视频无法在Webview中播放

时间:2013-09-11 11:07:18

标签: android youtube android-webview

我有一个使用ABS和Fragments实现的选项卡式应用程序。其中一个片段应该包含一个显示某个YouTube频道的网页视图,用户可以在其中查看频道的视频并观看,如果他喜欢的话。因此,我想在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);
    }
}

2 个答案:

答案 0 :(得分:0)

不要使用WebView播放YouTube视频?如果您使用Android YouTube API,则不需要它。有关详情,请参见Click Here

使用Android YouTube API,您可以将VID传递到YouTube Android播放器。该视频将在您的应用程序中播放

答案 1 :(得分:0)

终于在这里找到了我的解决方案...... https://stackoverflow.com/a/8490751/687475

设置网络浏览器客户端后,设置webview客户端两次以某种方式完成。 对我来说,显示完整频道的更简单的解决方案。