Display Youtube Video thumbnail on Android in easy way

compodoc

Recently I have implemented Youtube api in android application. One thing that google do is it provided a jar file with the help of which you can display youtube thumbnail.

But I find this method little bit more memory consuming plus in recyclerview you have to detach all references to avoid memory leakage during scrolling.

After scrolling many times it was not as smooth as must be. The main culprit was the code which download video thumbnail and display it.

So I decided to create UI for video view which was used inside a RecylerView like this

https://gist.github.com/CodeNextGen/8ff3315713f87b13df1f825144bcfa6f

Every youtube video has video id on basics of which thumbnail is fetched. So change I made in Java code was like this.

 try {
            String title = mCollections.get(position).title;
            String description = mCollections.get(position).post_content;
            String videoID = mCollections.get(position).link;
            collectionViewHolder.title.setText(Html.fromHtml(title));
            String imageUrl="http://img.youtube.com/vi/"+videoID+"/0.jpg";
            //Log.e("image url",imageUrl);
            Picasso.with(ctx).load(imageUrl).placeholder(R.drawable.loader).error(R.drawable.jambhgurutrans).into(collectionViewHolder.youtubeThumbnail);

        } catch (Exception e) {
            Picasso.with(ctx).load("http://www.bishnoinextgen.in/wordpress/wp-content/uploads/2017/01/jambhguru.png").into(collectionViewHolder.youtubeThumbnail);
        }

I am using Picaso library for image loading from network you can use whatever you like.

If you want to code in conventional way which was not working good for me you can follow on link given below

http://www.androhub.com/how-to-show-youtube-video-thumbnail-play-youtube-video-in-android-app/

Output

Download and run this app. On very first page , list with multiple type of item using recycler view is implemented
with youtube video, audio, images and many more view types.

https://play.google.com/store/apps/details?id=stayrocks.jambh.vani


Tagged with:
CodeNextGen
Written by Sanjeev Kumar

Get notifications about new posts on Twitter, RSS or Email.