Youtube Api android autostart

DzungPV picture DzungPV · Mar 12, 2013 · Viewed 25.5k times · Source

I use Youtube Api in my app, but my problem is it's not auto play, must press play button.

My code is:

setContentView(R.layout.playerview_demo);
((YouTubePlayerView)findViewById(R.id.youtube_view)).initialize(DEV_KEY, this);

youtube_view layout:

<com.google.android.youtube.player.YouTubePlayerView 
    android:id="@id/youtube_view" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" />

Answer

Herman Schaaf picture Herman Schaaf · Mar 14, 2013

What you are looking for is the Youtube API's loadVideo method. From the docs:

public abstract void loadVideo (String videoId)

Loads and plays the specified video.

You can use it like this:

@Override
 public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
    boolean wasRestored) {
  this.player = player;
  player.loadVideo(video.id); // where video.id is a String of a Youtube video ID
}

In a similar vein, there is also the cueVideo method, which adds the video to the playlist, but does not automatically start playing the video.