get Youtube Video Id - Android android.media

Android examples for android.media:Youtube

Description

get Youtube Video Id

Demo Code

import android.net.Uri;

public class Main {
  public static final String SHORT_YOUTUBE = "youtu.be/";
  public static final String LONG_YOUTUBE = "youtube.com/";

  public static String getYoutubeVideoId(String url) {
    String youtubeId = "";
    if (isYoutubeURL(url)) {
      if (url.contains(LONG_YOUTUBE)) {
        try {//from   ww w . j  a  v  a2s .co  m
          Uri uri = Uri.parse(url);
          youtubeId = uri.getQueryParameter("v");
        } catch (Exception e) {
          return null;
        }
      } else if (url.contains(SHORT_YOUTUBE)) {
        try {
          url = url.split(SHORT_YOUTUBE)[1];
          if (url.contains("?")) {
            url = url.split("/?")[0];
          }
          youtubeId = url;
        } catch (Exception e) {
          return null;
        }
      }
    }
    return youtubeId;
  }

  public static boolean isYoutubeURL(String url) {
    if (url.contains(SHORT_YOUTUBE) || url.contains(LONG_YOUTUBE))
      return true;
    return false;
  }

}

Related Tutorials