How to retrieve details of single video from youtube using videoID through Data API v3.0 in Android?

别等时光非礼了梦想. 提交于 2019-12-01 06:55:15

YouTube provides (at least) two official libraries relevant to your question:

As the name already suggests, the first library is specifically developed for the Android platform. Its focus is on enabling you to incorporate video playback functionality into an app by providing a player framework. If your goal is to enable users to simply play YouTube videos, then is probably easiest to implement. Do note that this library requires the official YouTube app to be installed on the device.

The second library is more generic (although there are separate instructions for using it on Android) and provides a wrapper around YouTube's Data API to make interfacing with it a little easier. Hence, it allows you to do basically everything you can also do with the web API. As such, it solves a different problem than the Android Player API and is more likely the way to go if you want full control over how you display video data in your own UI.

Your third option would be to do exactly what you did for your web-based solution: make the API call yourself, parse the response and bind up the relevant data to your UI components. Various networking libraries (i.e. Retrofit) can greatly simplify this process.

SoulRayder

Refer my post here. I just tried this method for my project and it works very nicely. You don't need the above code or any google api jar imports. Just replace the HTTP request with your HTTP request.

Output is returned in JSON, for which you can use a JSON parser jar to retrieve the title,thumbnails and other details you may require, as I have described in my answer there.

Try this:

protected void requestYoutubeVideos(String text) {
      try {
          youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() {
              public void initialize(HttpRequest request) throws IOException {
              }
          }).setApplicationName("My app name").build();

          // Define the API request for retrieving search results.
          YouTube.Search.List query = youtube.search().list("id");

          // Set your developer key from the Google Cloud Console for
          // non-authenticated requests. See:
          // https://cloud.google.com/console
          query.setKey(YOUTUBE_API_KEY);
          query.setQ(text);
          query.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);

          // To increase efficiency, only retrieve the fields that the
          // application uses.
          query.setFields("items(id)");
          query.setOrder("viewCount");

          // Restrict the search results to only include videos. See:
          // https://developers.google.com/youtube/v3/docs/search/list#type
          query.setType("video");

          SearchListResponse searchResponse = query.execute();
          List<SearchResult> list = searchResponse.getItems();
          Log.e("Youtube search", "list ===> " + list);

          //Get Info for each video id
          for (SearchResult video: list) {
              youtubeList.add(video);

              YouTube.Videos.List query2 = youtube.videos().list("id,contentDetails,snippet,statistics").setId(video.getId().getVideoId());
              query2.setKey(YOUTUBE_API_KEY);
              query2.setMaxResults((long) 1);
              query2.setFields("items(id,contentDetails,snippet,statistics)");

              VideoListResponse searchResponse2 = query2.execute();
              List<Video> listEachVideo = searchResponse2.getItems();
              Video eachVideo = listEachVideo.get(0);

          }

      } catch (GoogleJsonResponseException e) {
          Log.e("Youtube search", "There was a service error: " + e.getDetails().getCode() + " : "
                  + e.getDetails().getMessage());
      } catch (IOException e) {
          Log.e("Youtube search", "There was an IO error: " + e.getCause() + " : " + e.getMessage());
      } catch (Throwable t) {
          t.printStackTrace();
      }
  }

and do not forget to call it from another thread:

 new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            requestYoutubeVideos("Harry el Sucio Potter");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}).start();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!