考虑到我在列表视图中有一个对象,其中有一个YouTube videoId作为参数之一。我知道如何使用OnItemClickListener以及如何通过意图传递额外的信息。当我进入接收活动时,必须在onCreate方法中调用intent.getStringExtra(“ key”)。当我调用cueVideo(videoId)方法时,我需要在另一个方法onInitializationSuccess中检索到videoId。您不仅可以解释该怎么做,还可以解释为什么我可以理解自己的成长。

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();
}


}

最佳答案

声明一个成员变量,或者只是在其他方法中调用getIntent并在那里获取ID。


您能解释为什么吗?


因为您想使用onCreate之外的值

10-06 14:01