In order to create youtube video thumbnail in android we have to first initialize its view i.e. YouTubeThumbnailView in our xml file. On initializing YoutubeThumbnailView we have to add developer key and its InitializationSuccess listener with two override methods onThumbnailLoaded and onThumbnailError. Finally on youTubeThumbnailLoader we have to add setVideo method in which we have to pass video id. Below example will let you know how to implement this in android.
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginBottom="@dimen/ten_dp"
android:visibility="gone"/>
// Initializing video player with developer key
youtube_view.initialize(Config.DEVELOPER_KEY, new YouTubeThumbnailView.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, final YouTubeThumbnailLoader youTubeThumbnailLoader) {
youTubeThumbnailLoader.setVideo(videoId);
youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener() {
@Override
public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
youTubeThumbnailLoader.release();
Toast.makeText(getActivity(), "It's a valid youtube url.", Toast.LENGTH_SHORT).show();
}
@Override
public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
try {
Toast.makeText(getActivity(), "Not a valid youtube url.", Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
}
});
0 Comment(s)