Obtaining a YouTube video ID, including options

In my Android app, I have an embedded video on YouTube.

I get the Youtube video id from the source 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 is great for the original URL: http://www.youtube.com/watch?v=A7ry4cx6HfY

When I do this, the video loads and plays perfectly, but the quality is very poor. (240p or 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 this URL http://www.youtube.com/watch?v=A7ry4cx6HfY&vq=hd1080 , this parameter is ignored, and the quality is still bad.

How can I get a video in the best quality? Of course, assuming the video is available for this quality. Why is my parameter ignored?

+6
source share
2 answers

I solved this problem using the official Google Youtube API for Android.

Probably not the best idea - try to implement the receipt of the video yourself. Using the API does this for you, and it works just fine.

EDIT

Here's the API link: https://developers.google.com/youtube/android/player/

+1
source

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; } 

Here you can find all the parser code https://github.com/TheFinestArtist/YouTubePlayerActivity/blob/master/library/src/main/java/com/thefinestartist/ytpa/utils/YoutubeUrlParser.java

This is a useful open source that I made to play Youtube Video. https://github.com/TheFinestArtist/YouTubePlayerActivity

+2
source

All Articles