Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play YouTubePlayer programmatically not working

I am using the YouTube API to play a video in my app.

I want to play the video right when the user enters the fragment so I used the command player.play(); in order to play automatically. However this command doesn't seem to work...

Here is my code:

public class YouTubeVideoFragment extends YouTubePlayerFragment{

YouTubePlayer player;

public YouTubeVideoFragment()
{
}


@Override
public void onDestroy()
{
    // TODO Auto-generated method stub
    super.onDestroy();
    if(player!= null)
    {
        player.release();
    }

}

@Override
public void onStop()
{
    // TODO Auto-generated method stub
    super.onStop();
    if(player!= null)
    {
        player.release();
    }
}

public static YouTubeVideoFragment newInstance(String url)
{

    YouTubeVideoFragment f = new YouTubeVideoFragment();

    Bundle b = new Bundle();
    b.putString("url", url);

    f.setArguments(b);
    f.init();

    return f;
}

private void init()
{

    initialize("api_key", new OnInitializedListener() {

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

            Log.e("YouTubeFragment", "Error");
        }

        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored)
        {



            if (!wasRestored)
            {
                player.cueVideo(getArguments().getString("url"));
                player.play();//THIS DOESNT WORK!!!

            }
        }
    });
}

}

like image 726
user1163234 Avatar asked Mar 04 '14 15:03

user1163234


2 Answers

If you need to load and play a video right after initialization of a fragment, you should simply use player.loadVideo(videoId) instead of player.cueVideo(videoId) and override onLoaded() hook.

Have a look at the docs:

loadVideo(String videoId) Loads and plays the specified video.

cueVideo(String videoId) Loads the specified video's thumbnail and prepares the player to play the video, but does not download any of the video stream until play() is called.

like image 131
Metaphore Avatar answered Oct 23 '22 23:10

Metaphore


You have to call player.play() in onLoaded() of PlayerStateChangeListener(). So change onInitializationSuccess to this:

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored)
    {
        if (!wasRestored)
        {
            player.setPlayerStateChangeListener(new PlayerStateChangeListener() {

            @Override
            public void onVideoStarted() {
            }

            @Override
            public void onVideoEnded() {
            }

            @Override
            public void onLoading() {
            }

            @Override
            public void onLoaded(String videoId) {
                player.play();
            }

            @Override
            public void onError(ErrorReason reason) {
            }

            @Override
            public void onAdStarted() {
            }
        });
     }
like image 1
Pratim Avatar answered Oct 23 '22 23:10

Pratim