32

I use YouTube API in my app. My problem is, the video does not auto play, and the user has to press the play button to start playing.

My code:

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" />

How do I make the video start automatically?

Pang
  • 9,564
  • 146
  • 81
  • 122
DzungPV
  • 1,561
  • 3
  • 18
  • 24

6 Answers6

85

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.

Community
  • 1
  • 1
Herman Schaaf
  • 46,821
  • 21
  • 100
  • 139
5

Using loadVideo function.

public abstract void loadVideo (String videoId, int timeMillis)

Loads and plays the specified video. Playback will start at the specified time in the video.

videoId - The ID of the video to be played timeMillis - The time in milliseconds

FYR: https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayer#loadVideo(java.lang.String)reader

HTML5 developer
  • 237
  • 3
  • 2
3

You need to use player.cueVideo("video_id");

This will load the video, but it will not start auto playing. The video is auto playing because you're using player.loadVideo("video_id");.

Hope it helps.

allenski
  • 1,652
  • 4
  • 23
  • 39
AkshayT
  • 2,901
  • 2
  • 28
  • 32
1

I have solve this myself, then i post here:

 public void onInitializationSuccess(YouTubePlayer.Provider paramProvider, YouTubePlayer  paramYouTubePlayer, boolean paramBoolean)
 {if (!paramBoolean)paramYouTubePlayer.loadVideo(CLIP_LINK);}
DzungPV
  • 1,561
  • 3
  • 18
  • 24
1

The documentation clearly states...

Note: YouTube only counts playbacks that are initiated through the native play button.

but u could simply call youtubeplayer.loadVideo(VIDEO_ID,0) // 0 millis, start right away

Otherwise you could use CHROMELESS controls and add custom controls to it. Then it may work. All the best..

0

Check that autoplay feature is set to 1. Player will load initial video in autoplay by default.

Please refer Youtube Autoplay parameters for more.

onkar
  • 4,427
  • 10
  • 52
  • 89
Yasir Malik
  • 441
  • 2
  • 9