Android Obtener enlace de video de youtube

Hi am developing an android application and part of my application wants to parse song title to youtube and get the video link. It does not matter to get the 100% correct video. so how I retrieve data from youtube?

Can any one help me to find a solution its really help full for me.

Muchas Gracias

preguntado el 28 de agosto de 12 a las 11:08

3 Respuestas

The most common way to do it is using the Youtube data API, which will return XML/Json that you can parse to retrieve things like the video url.

Updated (2017/01/24) (v3)

Use the following call to search for YouTube videos using a search query:

It supports the following basic parameters for searching:

  • parte : Video data you want to retrieve in the search. For basic searches the recommended value is retazo.
  • q : The text you want to search for
  • clave : Your Google Developer API Key. This key can be obtained at the Google Developer API Console on the Credentials page of your application. Make sure to enable the Youtube Data API v3 on the application your key belongs to.

For more parameters see the Documentación de la API de Google

Using the Java library

On Android you can either do a HTTP request to the URL using the standard HTTP request classes available on the platform, or you can use the Google API Java Library como se muestra a continuación:

        YouTube youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
            public void initialize(HttpRequest request) throws IOException {

        String queryTerm = "A fun video"

        // Define the API request for retrieving search results.
        YouTube.Search.List search ="id,snippet");


        // Call the API and print first result.
        SearchListResponse searchResponse = search.execute();
        if(searchResponse.getItems().size() == 0)
           //No items found.
        SearchResult firstItem = searchResponse.getItems().get(0);

        ResourceId rId = firstItem.getId();
        // Confirm that the result represents a video. Otherwise, the
        // item will not contain a video ID.
        if (rId.getKind().equals("youtube#video")) {
            Thumbnail thumbnail = firstItem.getSnippet().getThumbnails().getDefault();

            Log.d("YOUTUBE_SAMPLE","Video Id" + rId.getVideoId());
            Log.d("YOUTUBE_SAMPLE","Title: " + firstItem.getSnippet().getTitle());
            Log.d("YOUTUBE_SAMPLE","Thumbnail: " + thumbnail.getUrl());

Respondido el 24 de enero de 17 a las 12:01

Ya no está disponible - Hatim

I have updated the answer with the new API call for Youtube searches - leon lucardie

You should look for the official Youtube API:

returns you Json you just have to parse.

Respondido 28 ago 12, 11:08

Hi Thanks all you guys for pointing me the way I want to fallow. I had come up with something finally and also like to share my expirance

According to the youtube we can request data as xml or json. I used json method for my implementation

you can get more information from youtube developer guide

above request "title_you_want_to_search" is the keyword you want to search. and we can customize the result by passing the extra parameters to the url.

  • "max-results" : mention how many results you want to get (In my case I just want only one)
  • "alt" : the format you want results Json or xml

First we need to request data from Youtube api and then we have to select which part of information we want to choose from array. In my case I used the "data" and "items" to get the videoid. after we tacking the videoId then we can make the video URL like this

String mVideoLink = ""+videoID; (I used following functions to get this thing done)

public String readYoutubeFeed(String songTitle) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
String url = ""+songTitle+"&max-results=1&v=2&alt=jsonc";
try {
    URLEncoder.encode(url, "UTF-8");
} catch (UnsupportedEncodingException e1) {
    Log.v(TAG,"encode error");
 HttpGet httpGet = new HttpGet(url);        
    try {
      HttpResponse response = client.execute(httpGet);
      StatusLine statusLine = response.getStatusLine();
      int statusCode = statusLine.getStatusCode();
      if (statusCode == 200) {
         HttpEntity entity = response.getEntity();
         InputStream content = entity.getContent();
         BufferedReader reader = new BufferedReader(new InputStreamReader(content, "UTF-8"));
        String line;
        while ((line = reader.readLine()) != null) {
      } else {
        Log.v(TAG,"Failed to download file");
    } catch (ClientProtocolException e) {
      Log.v(TAG,"readYoutubeFeed exeption1");
    } catch (IOException e) {
      Log.v(TAG,"readYoutubeFeed exeption2");
    return builder.toString();

public String getYouTubeVideoId(String songTitle){
String jesonData = readYoutubeFeed(songTitle);
String title = "123";        
try {       
    SONObject jObj = new JSONObject(jesonData); 
    JSONArray ja = jObj.getJSONObject("data").getJSONArray("items");
    JSONObject jo = (JSONObject) ja.get(0);
    title = jo.getString("id");              
    Log.v(TAG,"id is " +title);

} catch (Exception e) {
    Log.v(TAG,"error occerd");
return title;


One important thing want to mention in this converting strings to "UTF-8" is want to do because creating JsonArray may be throw exceptions. May be there are better ways to do this. If there any suggest

Respondido 30 ago 12, 07:08

Just want to say thanks, your code snippet provide a good reference for me. - Jeremy

it's No longer available api - Hatim

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.