Below given is the simple code snippet to play a video.
public class YoutubePlayerActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube_player);
playYouTubeVideo();
}
private void playYouTubeVideo()
{
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize("DummyKeyIzaSyBgURIAyTk3EklVgbtbiqN2Qw", this);
}
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
AppUtils.errorLog("Player initialization failure!");
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.cueVideo("wKJ9KzGQq0w");
player.play();
}
}
}
The player.play sometimes does not play it automatically. In that case, just call loadVideo instead of cueVideo or playVideo.
references:
https://developers.google.com/youtube/android/player/
No comments:
Post a Comment