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?
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 &.
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;
}
来源:https://stackoverflow.com/questions/28075116/youtube-video-playback-with-exoplayer