How to get / extract video id from an youtube url in android / java


public static String getYoutubeVideoId(String youtubeUrl)
 {
 String video_id="";
  if (youtubeUrl != null && youtubeUrl.trim().length() > 0 && youtubeUrl.startsWith("http"))
 {

String expression = "^.*((youtu.be"+ "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*"; // var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
 CharSequence input = youtubeUrl;
 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;
 }

About krishjlk

http://www.facebook.com/krishna.kanth.121

8 responses to “How to get / extract video id from an youtube url in android / java”

  1. The Finest Artist says :

    Doesn’t work well some how

  2. Phani says :

    thanks bro!

  3. Mehul says :

    This is very helpful …..

  4. Balamurugesh says :

    thanq,but video gets stuck and showing “this video is not available”.how to resolve it.

  5. Manikanta says :

    Very good example.

Leave a comment