在VideoView Android中播放YouTube视频

时间:2015-05-05 11:25:23

标签: android video-streaming youtube-data-api android-videoview

我搜索了很多以获取Youtube视频的流式网址,但这些都不起作用。 Youtube是否取消了对android videoview中流媒体视频的支持?

代码示例:

public String getYoutubeUriForVideoId(String videoId) {
        String url = "";
        try {
            Log.e(TAG, "Input video id: "+videoId);
                String YOUTUBE_VIDEO_INFORMATION_URL = 
                        "http://m.youtube.com/watch?ajax=1&layout=mobile&tsp=1&utcoffset=330&v="+videoId; 

                String USER_AGENT ="Mozilla/5.0 (Linux; U; Android 2.2.1; en-gb; GT-I9003 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
                Log.e(TAG, "Url: "+YOUTUBE_VIDEO_INFORMATION_URL);
                HttpClient lClient = new DefaultHttpClient();
                HttpGet lGetMethod = new HttpGet(YOUTUBE_VIDEO_INFORMATION_URL);
                // setting the header as to get the high quality video url 
                lGetMethod.setHeader("User-Agent", USER_AGENT);
                HttpResponse lResp = lClient.execute(lGetMethod);

                if(lResp == null)
                    return null;
                String lInfoStr = EntityUtils.toString(lResp.getEntity(), "UTF-8");
                Log.e(TAG, "Response: "+lInfoStr);
                JSONObject videoInfoJson = new JSONObject(lInfoStr.substring(4,lInfoStr.length()));
                JSONObject content = (JSONObject) videoInfoJson.get("content");
                JSONObject video = (JSONObject) content.get("video");
                JSONArray fmt_stream_map = video.getJSONArray("fmt_stream_map"); 
                url = fmt_stream_map.getJSONObject(0).getString("url");
        } catch(Exception e) {
            Log.e(TAG, e);
        }
        Log.e(TAG, "Youtube stream url: "+url);
            return url;
    }

1 个答案:

答案 0 :(得分:0)

你应该使用YouTubeAndroidApi为Android播放YouTube视频,这是非常简单的方法。

以下是示例link