How to display thumbnail of YouTube Videos in Android

前端 未结 5 2204
野的像风
野的像风 2020-12-07 23:42

I writing an app where i want to display a list of YouTube videos. But I want the list to display the video title with some other info but also show a thumbnail of the video

5条回答
  •  情深已故
    2020-12-07 23:57

    This question is old, but it is still coming in the top Google's search answers, so this us how I did it with the Android YouTube API.

    YouTubeThumbnailView thumbnail = findViewById(R.id.thumbnail);
    thumbnail.initialize(YOUTUBE_API_KEY, new YouTubeThumbnailView.OnInitializedListener() {
    
        @Override
        public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, 
                                            YouTubeThumbnailLoader youTubeThumbnailLoader) {
            youTubeThumbnailLoader.setVideo(youtubeID);
            youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener() {
    
                @Override
                public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
                    //need to release the loader!!!
                    youTubeThumbnailLoader.release();
                }
    
                @Override
                public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, 
                                             YouTubeThumbnailLoader.ErrorReason errorReason) {
                    //need to release the loader!!!
                    youTubeThumbnailLoader.release();
                }
            });
         }
    
         @Override
         public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, 
                                        YouTubeInitializationResult youTubeInitializationResult) {
         //handle error here
         }
    });
    

    YOUTUBE_API_KEY get your api key in here

    youtubeID You need to use the video's ID not full URL.

    Documentation here.

提交回复
热议问题