Restarting a Youtube Player View (Invalidation does not work)

1.4k views Asked by At

Im having some problems with the Youtube Player View. I have a listview where the user selects the video to be reproduced among a few.

It only works well the first time a video is clicked in the list view. The second time the Youtube Player View does not change at all. I´ve tried invalidating the Youtube Player View with invalidate and then calling again initalize but it does not work. This is the relevant code (the setOnItemClickListener of the listView):

videosListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> pariente, View view,
                int position, long id) {

            Video chosen = (Video) pariente.getItemAtPosition(position);
            String url = chosen.getUrl();
            IDVideo = getYoutubeVideoId(url);

            youtubeView.initialize(keyDeveloper,
                    new YouTubePlayer.OnInitializedListener() {

                        @Override
                        public void onInitializationFailure(Provider arg0,
                                YouTubeInitializationResult arg1) {

                        }

                        @Override
                        public void onInitializationSuccess(Provider arg0,
                                YouTubePlayer player, boolean wasRestored) {
                            if (!wasRestored) {
                                player.cueVideo(IDVideo);
                            }
                        }
                    });
        }
    });
}
1

There are 1 answers

0
MikeIsrael On BEST ANSWER

You shouldn't be re-initializing the playerview, just re-cueing. I actually did a tutoral that doesn't have a list view but three videos which can be switched out by clicking buttons. You can check out the video and source code here.

Also there is some code for using youtube thumbnails as well if you would want them for your lists.