33

我对使用ExoPlayer播放 YouTube 视频很感兴趣。我从ExoPlayer 示例中看到他们通过 DASH URL 播放 YouTube 视频。

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

4

1 回答 1

6

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

开始 url dashmpd,首先结束 url &

example(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-8ce/yvuxaxjvh-h 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,其中包含有关视频的信息。这个 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;
    }
于 2016-03-11T11:45:38.353 回答