In my Android application, I have an embedded YouTube video.
I'm getting the Youtube video ID from the original url as follows:
private String extractYoutubeId(String url) {
String video_id = "";
if (url != null && url.trim().length() > 0 && url.startsWith("http")) {
String expression = "^.*((youtu.be" + "\\/)"
+ "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*";
CharSequence input = url;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
String groupIndex1 = matcher.group(7);
if (groupIndex1 != null && groupIndex1.length() == 11)
video_id = groupIndex1;
}
}
return video_id;
}
This works just fine for an original url like this: http://www.youtube.com/watch?v=A7ry4cx6HfY
When I do that the video is loaded and played perfectly fine, but the quality is very bad. (240p or even worse, I think)
So from googling, I know that you just need to add a parameter like &vq=large
or &vq=hd1080
to get 480p/1080p.
But when I use a url like this http://www.youtube.com/watch?v=A7ry4cx6HfY&vq=hd1080 the parameter is ignored and the quality is still bad.
How can I get the video in better quality? Of course, assuming that the video is available in that quality. Why is my parameter being ignored?
The logic is change the last letter with the consecutive letter, the letters are case sensitive. So if the last letter is 'a' change it to 'b', if 'A' change it to 'B', if '1' change it to '2'.
Try this code here.
// (?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})
final static String reg = "(?:youtube(?:-nocookie)?\\.com\\/(?:[^\\/\\n\\s]+\\/\\S+\\/|(?:v|e(?:mbed)?)\\/|\\S*?[?&]v=)|youtu\\.be\\/)([a-zA-Z0-9_-]{11})";
public static String getVideoId(String videoUrl) {
if (videoUrl == null || videoUrl.trim().length() <= 0)
return null;
Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(videoUrl);
if (matcher.find())
return matcher.group(1);
return null;
}
You can find my whole parser code from here https://github.com/TheFinestArtist/YouTubePlayerActivity/blob/master/library/src/main/java/com/thefinestartist/ytpa/utils/YoutubeUrlParser.java
This is useful open source I made to play Youtube Video. https://github.com/TheFinestArtist/YouTubePlayerActivity
I solved this problem by using the official Google Youtube API for Android.
It's probably not the best idea to try implementing the getting of the videos by oneself. Using the API does that for you and it works just fine.
EDIT
Here the link to the API: https://developers.google.com/youtube/android/player/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With