YouTube video playback with ExoPlayer [closed]

2019-03-09 05:19发布

I am interested in using the ExoPlayer for YouTube video playback. I see from the ExoPlayer samples that they play YouTube videos via DASH URLs.

I'm using the Android YouTube API to search for videos, and don't see a means for obtaining a DASH URL for any of the search results. Is someone aware of any samples for integrating the YouTube API (v3) with the ExoPlayer (without using hardcoded video urls), or know of a way to get the information I need from the YouTube API to load the video into the ExoPlayer?

1条回答
太酷不给撩
2楼-- · 2019-03-09 05:36

For getting DASH URL, you need to download file: http://www.youtube.com/get_video_info?&video_id=" + videoId(sample "BU2zodCxwwo"). Parse file, get "dashmpd" and use it.

Start url dashmpd, end url first &.

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-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)

This URL for XML file, where information about the video. This URL little lives and not all videos contain format fmp4. If you use old URL or videos not contain fmp4 format, you take 403 error(Your client does not have permission to get URL). The solution to this problem, I have not found.

   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;
    }
查看更多
登录 后发表回答