請考慮我有一個對象,在視頻列表視圖中將YouTube videoId作爲其中一個參數。我知道如何使用OnItemClickListener以及如何通過意圖傳遞額外信息。當我到達接收活動時,我必須在onCreate方法中調用intent.getStringExtra(「key」)。我需要在另一種方法onInitializationSuccess中檢索videoId,因爲當我調用cueVideo(videoId)方法時。你能不能解釋一下怎麼做,但爲什麼我可以理解我自己的成長。傳遞Youtube視頻ID通過意圖
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();
}
}
您已經有了'getVideoId.getStringExtra',那麼問題是什麼? –
我有一個腦屁,我意識到我可以創建一個全局變量,在onCreate方法中初始化它,然後在onInitializationSuccess方法中調用它。但這就是我的問題所在。 – Sav
不用擔心。如果你願意,隨時接受我的答案 –