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;
}
Doesn’t work well some how
Sorry~~ It was totally my mistake!!!
This code works so fine~
Thank You!!!
I found that your RE doen’t work with video id start with ‘v’
I think I should change the RE, but added this code…
give me “UJl8MuemLw” string and returns null
else if (groupIndex1 != null && groupIndex1.length() == 10)
video_id = “v” + groupIndex1;
thanks bro!
wel 🙂
This is very helpful …..
thanq,but video gets stuck and showing “this video is not available”.how to resolve it.
Very good example.