Android Get a video link from youtube

Hi, I’m developing an application for Android, and part of my application wants to parse the song title on youtube and get a link to the video. It’s not necessary to receive 100% correct video. so how do i get data from youtube?

Can someone help me find a solution that really helps me.

thanks

+4
source share
3 answers

The most common way to do this is to use the Youtube data API, which will return XML / Json, which you can parse to get things like a video.

Updated (2017/01/24) (v3)

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

https://www.googleapis.com/youtube/v3/search?part=snippet&q=fun%20video&key=YOUR-API-KEY 

It supports the following basic search options:

  • part . The video data you want to search. For basic searches, the recommended snippet value.
  • q : the text you want to find
  • key : your Google developer API key. This key can be obtained from the Google Developer API Console on your application credentials page. Be sure to enable Youtube Data API v3 in the application to which your key belongs.

For more options, see the Google API Documentation

Using the Java library

On Android, you can either execute the HTTP request at the URL using the standard HTTP request classes available on the platform, or you can use the Google API Java Library , as shown below:

  YouTube youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() { public void initialize(HttpRequest request) throws IOException { } }).setApplicationName("YOUR-APPLICATION-NAME").build(); String queryTerm = "A fun video" // Define the API request for retrieving search results. YouTube.Search.List search = youtube.search().list("id,snippet"); search.setKey("Your-Api-Key"); search.setQ(queryTerm); // Call the API and print first result. SearchListResponse searchResponse = search.execute(); if(searchResponse.getItems().size() == 0) { //No items found. return; } 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()); } 
+3
source

You should look for the official Youtube API:

https://developers.google.com/youtube/code?hl=fr#Java

returns you the Json you just need to parse.

+1
source

Hi, Thank you guys for telling me how I want to fall. I finally came up with something and also wanted to share my experience.

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

http://gdata.youtube.com/feeds/api/videos?q=title_you_want_to_search&max-results=1&v=2&alt=jsonc

you can get more information from the youtube developer guide

above, the query "title_you_want_to_search" is the keyword you want to find. and we can customize the result by passing additional parameters to the URL.

  • "max-results": indicate how many results you want to get (in my case, I just want only one)
  • "alt": desired json or xml result format

First we need to request data from the Youtube api, and then we must choose which part of the information we want to select from the array. In my case, I used "data" and "elements" to get a video game. after we turn on the video, then we can make the video URL as follows

String mVideoLink = "https://youtu.be/"+videoID; (I used the following functions to do this)

 public String readYoutubeFeed(String songTitle) { StringBuilder builder = new StringBuilder(); HttpClient client = new DefaultHttpClient(); String url = "http://gdata.youtube.com/feeds/api/videos?q="+songTitle+"&max-results=1&v=2&alt=jsonc"; try { URLEncoder.encode(url, "UTF-8"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); 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) { builder.append(line); } } else { Log.v(TAG,"Failed to download file"); } } catch (ClientProtocolException e) { e.printStackTrace(); Log.v(TAG,"readYoutubeFeed exeption1"); } catch (IOException e) { e.printStackTrace(); Log.v(TAG,"readYoutubeFeed exeption2"); } return builder.toString(); } public String getYouTubeVideoId(String songTitle){ String jesonData = readYoutubeFeed(songTitle); Log.i(TAG,jesonData); 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) { e.printStackTrace(); Log.v(TAG,"error occerd"); } return title; 

}

One important thing to mention in this conversion of strings to "UTF-8" is that creating JsonArray may be an exception to the exception. Maybe there are better ways to do this. If there is an offer

+1
source

All Articles