-
StackOverflow 文档
-
Android 教程
-
YouTube API
-
延长 YouTubeBaseActivity 的活动
public class CustomYouTubeActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener, YouTubePlayer.PlayerStateChangeListener {
private YouTubePlayerView mPlayerView;
private YouTubePlayer mYouTubePlayer;
private String mVideoId = "B08iLAtS3AQ";
private String mApiKey;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mApiKey = Config.YOUTUBE_API_KEY;
mPlayerView = new YouTubePlayerView(this);
mPlayerView.initialize(mApiKey, this); // setting up OnInitializedListener
addContentView(mPlayerView, new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT)); //show it in full screen
}
//Called when initialization of the player succeeds.
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player,
boolean wasRestored) {
player.setPlayerStateChangeListener(this); // setting up the player state change listener
this.mYouTubePlayer = player;
if (!wasRestored)
player.cueVideo(mVideoId);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
Toast.makeText(this, "Error While initializing", Toast.LENGTH_LONG).show();
}
@Override
public void onAdStarted() {
}
@Override
public void onLoaded(String videoId) { //video has been loaded
if(!TextUtils.isEmpty(mVideoId) && !this.isFinishing() && mYouTubePlayer != null)
mYouTubePlayer.play(); // if we dont call play then video will not auto play, but user still has the option to play via play button
}
@Override
public void onLoading() {
}
@Override
public void onVideoEnded() {
}
@Override
public void onVideoStarted() {
}
@Override
public void onError(ErrorReason reason) {
Log.e("onError", "onError : " + reason.name());
}
}