我正在按照本教程通过 YouTube API 设置自定义 YouTube 播放器:http ://www.techrepublic.com/blog/android-app-builder/using-googles-youtube-api-in-your-android-apps /
这是加载预设 YouTube 视频的代码:
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
static private final String DEVELOPER_KEY = "mykey";
static private final String VIDEO = "Yc8YrVc47TI";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(DEVELOPER_KEY, this);
}
@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult error) {
Toast.makeText(this, "Oh no! "+error.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
player.loadVideo(VIDEO);
}
}
现在我正在尝试通过单击一个按钮来加载不同的 YouTube 视频。所以我onClickListener
为我设置了一个button1
并尝试访问 YouTube 播放器,但它不起作用。
public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
static private final String DEVELOPER_KEY = "mykey";
static private final String VIDEO = "Yc8YrVc47TI";
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(DEVELOPER_KEY, this);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
youTubeView.loadVideo("Erd2k6EKxCQ");
}
});
}
@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult error) {
Toast.makeText(this, "Oh no! "+error.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
player.loadVideo(VIDEO);
}
}
如何将特定视频从 button1 的 onClickListener 加载到 YouTubePlayer player.component?