Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to generate video id from youtube video url in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.57k
    Comment on it

    If you are using YouTubePlayer in your android app and wants to play youtube video on that, so first we need a youtube video url. From that url we can get a video id(11 alphanumeric character set). In below example we are going to generate a video id from a url. By passing a video url we first compile it with a particular expression, if the pattern matches we will get a video id otherwise we get a null value.

     

    This will work on two kinks of youtube video urls either direct or shared url.
    Direct url: https://www.youtube.com/watch?v=XXXXXXXXXXX
    Shared url: https://youtu.be/XXXXXXXXXXX

    private final static String expression = "(?<=watch\\?v=|/videos/|embed\\/|youtu.be\\/|\\/v\\/|\\/e\\/|watch\\?v%3D|watch\\?feature=player_embedded&v=|%2Fvideos%2F|embed%\u200C\u200B2F|youtu.be%2F|%2Fv%2F)[^#\\&\\?\\n]*";
    
    public static String getVideoId(String videoUrl) {
            if (videoUrl == null || videoUrl.trim().length() <= 0){
                return null;
    	}
    
            Pattern pattern = Pattern.compile(expression);
            Matcher matcher = pattern.matcher(videoUrl);
    
            try {
                if (matcher.find())
                    return matcher.group();
            } catch (ArrayIndexOutOfBoundsException ex) {
    		ex.printStackTrace();
            }
            return null;
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: