Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh Youtube Player View onItemClickListener() in a ListView

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);
        }

    }
like image 887
Sophie Avatar asked Dec 01 '15 10:12

Sophie


1 Answers

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 method

YouTubePlayerFragment youTubePlayerFragment = YouTubePlayerFragment.newInstance();
    youTubePlayerFragment.initialize(API_KEY, this);

In the initialization listener you implemented in Activity, You should keep the reference of YoutubePlayer in a class level attribute like this

 //your class level attribute to keep reference of player
   YouTubePlayer mYoutubePlayer;

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {

    if (!b) {
        youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
        youTubePlayer.cuePlaylist(PLAYLIST_ID);
        //Save reference of initialized player in class level attribute
         mYoutubePlayer = youTubePlayer;
    }

}

And use this player attribute to load videos inside onItemClick instead of calling initialize again on YoutubePlayerFragment with new listener

 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
        if(mYoutubePlayer!=null){
        mYoutubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
        mYoutubePlayer.loadVideo(strVideoID);
        mYoutubePlayer.play();
        }

    }
});
like image 173
Umar Qureshi Avatar answered Oct 20 '22 20:10

Umar Qureshi