When I first call this method I get loaded youtube video. But the second time when I call the method, my old youtube video remains. It looks like it does not initialize again.
private void showRoomInformations(final RoomDTO room){
//Verify if the number of connections is the same
if (room != null){
YouTubePlayer.OnInitializedListener mOnInitializedListener;
YouTubePlayer.PlayerStateChangeListener mPlayerStateChangeListener;
YouTubePlayerView mYouTubePlayerView;
mRoomDTO = room;
TextView roomDescription = (TextView) ((Activity)mContext).findViewById(R.id.textDescription);
TextView textRoomName = (TextView) ((Activity)mContext).findViewById(R.id.textRoomName);
mYouTubePlayerView = (YouTubePlayerView) ((Activity)mContext).findViewById(R.id.view_youtube);
textRoomName.setText(room.getName());
roomDescription.setText(room.getDescription());
mOnInitializedListener = new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
Log.d(TAG, "onClick: Done initializing.");
youTubePlayer.cueVideo(getYouTubeUrl(room.getMedia()));
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.d(TAG, "onClick: Failed to initialize.");
}
};
mYouTubePlayerView.initialize(YouTubeConfig.getApiKey(), mOnInitializedListener);
}
}
private String getYouTubeUrl(String youTubeUrl){
return youTubeUrl.replace("https://youtu.be/", "");
}
Can you please tell me what am I doing wrong? How to initialize YouTubePlayer again?
You need to initialize the player only once. After that you should use YouTubePlayer.loadVideo()
or YouTubePlayer.cueVideo()
.