如何在android中结束1个视频后播放下一个youtube视频

时间:2016-07-15 10:20:47

标签: android android-youtube-api android-video-player

我想在Android中集成YouTube播放器后播放多个视频。在YouTube播放器结束1个视频后,我需要播放另一个视频,我搜索了很多但是没有得到任何正确的结果。我不知道我是怎么做的,请帮助我。

public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

private YouTubePlayer YPlayer;
private static final String YoutubeDeveloperKey = "AIzaSyB2nIJ2lGZaCcvAq7a2ZY6Ny4lzjUhQld4";
private static final int RECOVERY_DIALOG_REQUEST = 1;
int count = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(YoutubeDeveloperKey, this);
     }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
YPlayer = youTubePlayer;
         /*
          * Now that this variable YPlayer is global you can access it
          * throughout the activity, and perform all the player actions like
          * play, pause and seeking to a position by code.
          */
if (!b) {
YPlayer.cueVideo("wPxqcq6Byq0");
YPlayer.cueVideo("wPxqcq6Byq0");

         }
     }

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
 YouTubeInitializationResult errorReason) {
  if (errorReason.isUserRecoverableError()) {
   errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
    } 
else { String errorMessage = String.format(
"There was an error initializing the YouTubePlayer",
errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
         }
     }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
getYouTubePlayerProvider().initialize(YoutubeDeveloperKey, this);
         }
     }

protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
     }

@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
MultiDex.install(this);
     }

private 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) {

         }
     };
 }

1 个答案:

答案 0 :(得分:0)

NextAsyncTask覆盖方法执行onVideoEnded。 当前视频结束时调用此回调方法。

@Override
    public void onVideoEnded() {

     // do something here
     // play next video here
    }