I got this code to get the youtube id from the links like www.youtube.com/watch?v=xxxxxxx
URL youtubeURL = new URL(link);
youtubeURL.getQuery();
This will work me and simple
public static String getVideoId(@NonNull String videoUrl) {
String reg = "(?:youtube(?:-nocookie)?\\.com\\/(?:[^\\/\\n\\s]+\\/\\S+\\/|(?:v|e(?:mbed)?)\\/|\\S*?[?&]v=)|youtu\\.be\\/)([a-zA-Z0-9_-]{11})";
Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(videoUrl);
if (matcher.find())
return matcher.group(1);
return null;
}