在列表视图中,我有一个对象,youtube videoid是其中一个参数。我知道如何使用OnItemClickListener以及如何通过意图传递额外的。当我参加接收活动时,我必须在OnCreate方法中调用Intent.getStringExtra(" key")。我需要用另一种方法(onitializationuccess)检索到的videoid,因为当我调用cuevideo(vaceoid)方法时。您能否不仅解释该怎么做,还可以解释为什么我可以理解自己的成长。
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。
您可以解释[...]为什么
因为您想使用ongreate
我一直使用的东西及其正常工作。
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.
}
}
}
打电话给这个
Intent intent = new Intent(mContext, VideoPlayerActivity.class);
intent.putExtra(Constants.YOUTUBE_VIDEO_KEY, youtubeKey);
mContext.startActivity(intent);
也在常量中。Java文件
public static final String YOUTUBE_VIDEO_KEY = "you_tube_video_key";
public static final String YOUTUBE_API_KEY = "YOUR_API_KEY";
希望这对您有帮助