否可以在Android中使用WebView播放Youtube视频

否可以在Android中使用WebView播放Youtube视频

本文介绍了是否可以在Android中使用WebView播放Youtube视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了许多在Google上找到的教程,但没有一个答案真正起作用.我希望视频在WebView中播放,而不是通过YouTube应用程序播放.任何帮助都是极好的.谢谢.

I've gone though many tutorials found on Google and none of the answers really work. I would like the video to play in the WebView instead of going though the YouTube app. Any help would be awesome. Thanks.

代码:

 public class Youtube extends Activity {
WebView myWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.website);

    myWebView = (WebView) findViewById(R.id.webView);
    myWebView.loadUrl("http://www.youtube.com");

    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
    myWebView.setWebViewClient(new WebViewClient());
    myWebView.getSettings().setBuiltInZoomControls(true);
    myWebView.getSettings().setSupportZoom(true);
    myWebView.getSettings().setUseWideViewPort(true);
    myWebView.getSettings().setLoadWithOverviewMode(true);

    getActionBar().setDisplayHomeAsUpEnabled(true);
}

public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost()
                    .equals("http://www.youtube.com")) {
                // This is my web site, so do not override; let my WebView
                // load the page
                return false;
            }
            // Otherwise, the link is not for a page on my site, so launch
            // another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }

    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up
    // to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);

        return true;
    default:
        return super.onOptionsItemSelected(item);

    }
}
 }

推荐答案

使用新的YouTube Android Player API- https://developers.google.com/youtube/android/player

Use the new YouTube Android Player API - https://developers.google.com/youtube/android/player

" API定义了用于加载和播放YouTube视频(和播放列表)以及自定义和控制视频播放体验的方法."

因此,要与频道一起使用,您将使用常规的YoutTube API来获取元数据,并使用Player API播放视频/播放列表.

So for using with a Channel, you'd use the regular YoutTube API to get the metadata, and play the videos/playlists with the Player API.

这篇关于是否可以在Android中使用WebView播放Youtube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:06