1
votes

I´m using the Youtube Player API for Android to play videos selecting one of these in a listview (videos are previously got parsing json etc etc). The problem comes when I can only play the first video of the listview that I select, cause the player view doesnt change when I do click in a different element of the listView. This is the important code:

 lista.setOnItemClickListener(new OnItemClickListener(){

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

                Video chosen = (Video)pariente.getItemAtPosition(posicion); 
                String urlVideo = chosen.getUrl();
                String aux = getYoutubeVideoId(urlVideo);
                URL_VIDEO = aux;

                youTubeView.initialize(KEY_DEVELOPER, new YouTubePlayer.OnInitializedListener() {

                    @Override
                    public void onInitializationFailure(Provider arg0,
                            YouTubeInitializationResult arg1) {
                        // TODO Auto-generated method stub
                        Log.d("DEPURANDO: ERROR AL VISUALIZAR", URL_VIDEO);

                    }

                    @Override
                    public void onInitializationSuccess(Provider arg0,
                            YouTubePlayer player, boolean wasRestored) {
                        // TODO Auto-generated method stub
                        if(!wasRestored){       
                            Log.d("YOUTUBE", "URL: " + URL_VIDEO);
                            player.cueVideo(URL_VIDEO);
                        }
                    }
                });
            }

I understand the problem comes because the player can't be initialized twice, so the execution only enter once in OnInitializationSucess (I tested) and therefore only plays the first video. how to solve this problem?

4
I've tried making a player in listview type of application, which I failed miserably. When you play your first video in the listview, have you tried scrolling the list? In my phone it leaves a black rectangle everywhere in the listview when I'm scrolling, does same thing happens to you?Chor Wai Chun
Mmmm no, I dont get the same error, after cliking ina video the listview keep working fine!Rafag

4 Answers

3
votes

Check the last version of the YouTube API Samples

YouTube API Samples

I think this sample can be useful to you:

youtube-api-samples

4
votes

You just need to initialize once, and store the player in your class

@Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer player, boolean wasRestored) {
     if(!wasRestored){       
         mPlayer = player;
     }
}

After that, you could play any Youtube URL on your mPlayer by calling the function.

public void playVideo(String url) {
     if( mPlayer != null ) {
          mPlayer.cueVideo(url);
     }
}
0
votes

On click, just scrollListToPosition(position) & if the item is last item in list then you can give padding by calculating the top of item. Alternatively use YouTubeStandalonePlayer to play video

Intent intent = YouTubeStandalonePlayer.createVideoIntent((Activity) mContext, YOU_TUBE_API_KEY, VideoID[getLayoutPosition()], 100, false, true);

mContext.startActivity(intent);
0
votes

I know it's very old, but solution for people coming after this post.

First initialise YoutubePlayerView and store YoutubePlayer object in class variable.

youTubeView.initialize(KEY_DEVELOPER, new YouTubePlayer.OnInitializedListener() {

                @Override
                public void onInitializationFailure(Provider arg0,
                        YouTubeInitializationResult arg1) {
                    // TODO Auto-generated method stub
                    Log.d("DEPURANDO: ERROR AL VISUALIZAR", URL_VIDEO);

                }

                @Override
                public void onInitializationSuccess(Provider arg0,
                        YouTubePlayer player, boolean wasRestored) {
                    youtubePlayer = player;
                }
            });

here "youtubePlayer" is a variable of YoutubePlayer class.

Now inside onclick listener load your video in youtubePlayer

lista.setOnItemClickListener(new OnItemClickListener(){

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

            Video chosen = (Video)pariente.getItemAtPosition(posicion); 
            String urlVideo = chosen.getUrl();
            String aux = getYoutubeVideoId(urlVideo);
            URL_VIDEO = aux;
            youtubePlayer.load(URL_VIDEO);
        }