Youtube Video Playback With Exoplayer
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
Solution 1:
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.
publicstaticObservable<String> downloadSound(String youtubeUrl, final String baseDir) {
returnDownloadManager.downloadFile("http://www.youtube.com/get_video_info?&video_id=" + youtubeUrl, baseDir + File.separator + FILEYOUTUBE, newDownloadManager.IDownloadProgress() {
@OverridepublicvoidonProgress(long current, long max) {
}
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.map(newFunc1<File, String>() {
@OverridepublicStringcall(File youtubeFile) {
String dashmpd = null;
BufferedReader br = null;
try {
br = newBufferedReader(newFileReader(youtubeFile.toString()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
StringBuilder sb = newStringBuilder();
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;
}
});
}
publicstaticStringgetQueryParams(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;
}
Post a Comment for "Youtube Video Playback With Exoplayer"