使用ExoPlayer播放YouTube视频

时间:2015-01-21 19:20:56

标签: android youtube-api android-youtube-api exoplayer

我有兴趣使用ExoPlayer进行YouTube视频播放。我从ExoPlayer samples看到他们通过DASH网址播放YouTube视频。

我正在使用Android YouTube API搜索视频,并且没有找到获取任何搜索结果的DASH URL的方法。是否有人知道将YouTube API(v3)与ExoPlayer集成的任何示例(不使用硬编码的视频网址),或者知道如何从YouTube API获取我需要的信息以将视频加载到ExoPlayer中?

1 个答案:

答案 0 :(得分:6)

要获取DASH URL,您需要下载文件:http://www.youtube.com/get_video_info?&video_id=“+ videoId(示例”BU2zodCxwwo“)。解析文件,获取”dashmpd“并使用它。

启动网址 dashmpd ,首先结束网址&

  

例(dashmpd = http://manifest.googlevideo.com/api/manifest/dash/fexp/3300133%2C3300164%2C3312381%2C9416126%2C9418044%2C9418777%2C9419452%2C9420096%2C9420452%2C9422596%2C9423291%2C9423455%2C9423661%2C9423662%2C9426963%2C9427247%2C9427888%2C9428559%2C9428564%2C9429237%2C9429515/mm/31/source/youtube/expire/1456756908/itag/0/upn/5xR9ZCMatkY/mn/sn-ov8vuxaxjvh-v8ce/key/yt6/ipbits/0/hfr/1/id/o-AIiY1RGtClDFVTCNTuhp8pRSGDPgiBHby0Il52tFnHix/sparams/as%2Chfr%2Cid%2Cip%2Cipbits%2Citag%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cplayback_host%2Csource%2Cexpire/sver/3/pl/16/s/3640F01A917CDAD260DD0BB27CE627BB9A113ED1.3CEF418955ADD2F9C2048C289AD8D0E1FB6D5F034034/ms/au/mv/m/playback_host/r1---sn-ov8vuxaxjvh-v8ce.googlevideo.com/mt/1456735204/as/fmp4_audio_clear%2Cwebm_audio_clear%2Cfmp4_sd_hd_clear%2Cwebm_sd_hd_clear%2Cwebm2_sd_hd_clear/ip/37.193.113.79

此XML文件的URL,其中包含有关视频的信息。此网址很少,并非所有视频都包含格式fmp4。如果您使用旧URL或不包含fmp4格式的视频,则会出现403错误(您的客户端无权获取URL)。这个问题的解决方案,我还没有找到。

   public static Observable<String> downloadSound(String youtubeUrl, final String baseDir) {

        return DownloadManager.downloadFile("http://www.youtube.com/get_video_info?&video_id=" + youtubeUrl, baseDir + File.separator + FILEYOUTUBE, new DownloadManager.IDownloadProgress() {
            @Override
            public void onProgress(long current, long max) {

            }
        })
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<File, String>() {
                    @Override
                    public String call(File youtubeFile) {
                        String dashmpd = null;
                        BufferedReader br = null;
                        try {
                            br = new BufferedReader(new FileReader(youtubeFile.toString()));
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        try {
                            StringBuilder sb = new StringBuilder();
                            String line = br.readLine();

                            while (line != null) {
                                sb.append(line);
                                line = br.readLine();
                            }

                            String everything = sb.toString();

                            Log.d("TAG", everything);
                            dashmpd = getQueryParams(everything);

                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                br.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        return dashmpd;
                    }
                });
    }

    public static String getQueryParams(String url) {
        String dashUrl1 = url.substring(url.lastIndexOf("dashmpd"));
        String dashUrl2 = dashUrl1.substring(dashUrl1.lastIndexOf("dashmpd"), dashUrl1.indexOf("&"));
        String dashUrl = null;
        try {
            dashUrl = URLDecoder.decode(dashUrl2.substring(dashUrl2.indexOf("http")), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return dashUrl;
    }