YouTube API Android auto start(YouTube API Android 自动启动)

本文介绍了YouTube API Android 自动启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用 YouTube API.我的问题是,视频不会自动播放,用户必须按播放按钮才能开始播放.

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.

我的代码:

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

youtube_view 布局:

<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?

推荐答案

你要找的是 Youtube API 的 loadVideo 方法.来自 文档:

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.

你可以这样使用它:

@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
}

类似地,还有 cueVideo 方法,它将视频添加到播放列表中,但不会自动开始播放视频.

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.

这篇关于YouTube API Android 自动启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!