使用ExoPlayer播放YouTube视频



我有兴趣使用ExoPlayer播放YouTube视频。 我从ExoPlayer样本中看到他们通过DASH URL播放YouTube视频。

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

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

开始网址破折号,结束网址在前 &.

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%2C9428555% 9%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,其中有关视频的信息。这个 URL 几乎没有生命,并非所有视频都包含格式 fmp4。如果您使用旧网址或不包含 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;
    }

相关内容

  • 没有找到相关文章

最新更新