Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

YouTube API Android auto start

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...