Reputation: 23
Consider that I have an object, with a YouTube videoId as one of the parameters, in a list view. I know how to use the OnItemClickListener and how to pass an extra through an intent. When I get to the receiving Activity I have to call intent.getStringExtra("key") in the onCreate method. I need that retrieved videoId in another method, onInitializationSuccess, for when I call the cueVideo(videoId) method. Can you explain not only what to do but why so I can understand for my own growth.
public class PlayVideo extends YouTubeBaseActivity
implements YouTubePlayer.OnInitializedListener{
private String GOOGLE_API_KEY = "AIzaSyCKcPfcvaFtN_izxXg7lzEcKVdF0A-7y1Q";
String VIDEO_ID = "ZHAe-SaplCw";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_video);
YouTubePlayerView playerView = (YouTubePlayerView) findViewById(R.id.player_view);
playerView.initialize(GOOGLE_API_KEY, this);
//Get videoId from current ListView Object
Intent getVideoId = getIntent();
String videoId = getVideoId.getStringExtra("videoIdExtra");
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
Toast.makeText(this, "Video Playing", Toast.LENGTH_SHORT).show();
youTubePlayer.setPlayerStateChangeListener(playerStateChangeListener);
youTubePlayer.setPlaybackEventListener(playbackEventListener);
if(!wasRestored){
youTubePlayer.cueVideo(VIDEO_ID);
}
}
private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() {
@Override
public void onPlaying() {
}
@Override
public void onPaused() {
}
@Override
public void onStopped() {
}
@Override
public void onBuffering(boolean b) {
}
@Override
public void onSeekTo(int i) {
}
};
YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
@Override
public void onLoading() {
}
@Override
public void onLoaded(String s) {
}
@Override
public void onAdStarted() {
}
@Override
public void onVideoStarted() {
}
@Override
public void onVideoEnded() {
}
@Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
}
};
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Toast.makeText(this, "Play Failed", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 0
Views: 950
Reputation: 5049
What I have been using and its working fine.
public class VideoPlayerActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_REQUEST = 1;
private YouTubePlayerView youTubeView;
private MyPlayerStateChangeListener playerStateChangeListener;
private MyPlaybackEventListener playbackEventListener;
private YouTubePlayer player;
private String videoKey;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(Constants.YOUTUBE_API_KEY, this);
playerStateChangeListener = new MyPlayerStateChangeListener();
playbackEventListener = new MyPlaybackEventListener();
Bundle bundle = getIntent().getExtras();
videoKey = bundle.getString(Constants.YOUTUBE_VIDEO_KEY, null);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
this.player = player;
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
if (!wasRestored) {
player.cueVideo(Strings.isNullOrEmpty(videoKey) ? "fhWaJi1Hsfo" : videoKey);
// Plays https://www.youtube.com/watch?v=fhWaJi1Hsfo
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_REQUEST).show();
} else {
String error = String.format(getString(R.string.player_error), errorReason.toString());
Toast.makeText(this, error, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(Constants.YOUTUBE_API_KEY, this);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return youTubeView;
}
private final class MyPlaybackEventListener implements YouTubePlayer.PlaybackEventListener {
@Override
public void onPlaying() {
// Called when playback starts, either due to user action or call to play().
}
@Override
public void onPaused() {
// Called when playback is paused, either due to user action or call to pause().
}
@Override
public void onStopped() {
// Called when playback stops for a reason other than being paused.
}
@Override
public void onBuffering(boolean b) {
// Called when buffering starts or ends.
}
@Override
public void onSeekTo(int i) {
// Called when a jump in playback position occurs, either
// due to user scrubbing or call to seekRelativeMillis() or seekToMillis()
}
}
private final class MyPlayerStateChangeListener implements YouTubePlayer.PlayerStateChangeListener {
@Override
public void onLoading() {
// Called when the player is loading a video
// At this point, it's not ready to accept commands affecting playback such as play() or pause()
}
@Override
public void onLoaded(String s) {
// Called when a video is done loading.
// Playback methods such as play(), pause() or seekToMillis(int) may be called after this callback.
player.play();
}
@Override
public void onAdStarted() {
// Called when playback of an advertisement starts.
}
@Override
public void onVideoStarted() {
// Called when playback of the video starts.
}
@Override
public void onVideoEnded() {
// Called when the video reaches its end.
}
@Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
// Called when an error occurs.
}
}
}
To call this
Intent intent = new Intent(mContext, VideoPlayerActivity.class);
intent.putExtra(Constants.YOUTUBE_VIDEO_KEY, youtubeKey);
mContext.startActivity(intent);
also in Constants.java file
public static final String YOUTUBE_VIDEO_KEY = "you_tube_video_key";
public static final String YOUTUBE_API_KEY = "YOUR_API_KEY";
hope this helps you out
Upvotes: 0
Reputation: 191743
Either declare a member variable, or just call getIntent
in that other method and get the ID there.
Can you explain [...] why
Because you want to use the value outside of onCreate
Upvotes: 1