I am able to play Youtube videos using cuePlaylist()
but I also want to allow user to tap on any of the list item and then I want to refresh YoutubePlayerView
with the video user just tapped
I am using cuePlaylist()
so getting previous and next buttons as default functionality of Youtube player
So can I refresh YoutubePlayerView
with the one I have selected in a ListView
?
Here is my complete code, still when I do tap on any of the list item, not getting any change in YoutubePlayerView
but able to Log
video Id which I just clicked in a ListView
...
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
strVideoID = flowerList.get(i).getUrl();
Log.d("url:", strVideoID); // getting particular video id
youTubePlayerFragment.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
// YouTubeプレーヤーの初期化成功
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
player.loadVideo(strVideoID);
player.play();
}
}
// YouTubeプレーヤーの初期化失敗
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult error) {
// YouTube error
String errorMessage = error.toString();
Toast.makeText(MainActivity.this, errorMessage, Toast.LENGTH_LONG).show();
Log.d("errorMessage:", errorMessage);
}
});
}
});
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if (!b) {
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
youTubePlayer.cuePlaylist(PLAYLIST_ID);
}
}
step to play any you tube video
1 create a you tube view in xml file.
inside java file
You are using the wrong approach here, you don't need to call initialize on
YoutubePlayerFragment
each time, the first initialization is enough which you done in onCreate methodIn the initialization listener you implemented in Activity, You should keep the reference of YoutubePlayer in a class level attribute like this
And use this player attribute to load videos inside onItemClick instead of calling initialize again on YoutubePlayerFragment with new listener