停用时,Youtube API会崩溃

时间:2017-11-21 05:57:50

标签: java android android-studio crash youtube-api

当我处于暂停状态时,Youtube API会崩溃。 当我正在播放Youtube视频onclick它工作正常,但当我在暂停状态下最小化我的应用程序时它会崩溃。所以给我一些建议。我之前从未使用过Youtube API,因此我对此一无所知。请查看我的代码是否合适。如果没有,那么请提供好的 建议也是。提前谢谢。

    package com.app.embedyoutube;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

    import com.google.android.youtube.player.YouTubeBaseActivity;
    import com.google.android.youtube.player.YouTubeInitializationResult;
    import com.google.android.youtube.player.YouTubePlayer;
    import com.google.android.youtube.player.YouTubePlayerView;

    public class MainActivity extends YouTubeBaseActivity implements 
    YouTubePlayer.OnInitializedListener {

    String key="AIzaSyASLaX32-EG2Rx0EWZFPbwhuBDgsBgC6e0";
    YouTubePlayer youTubePlayer1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final YouTubePlayerView youtubePlayerView = findViewById(R.id.youtubePlayerView);
        youtubePlayerView.initialize(key,this);
        findViewById(R.id.b1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            play("ScQrLSBAuQk");
            }
        });
        findViewById(R.id.b2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                play("Ow9-fzf4Qbk");
            }
        });


    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {

            youTubePlayer1=youTubePlayer;
        youTubePlayer1.cueVideo("Ow9-fzf4Qbk");
        youTubePlayer1.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {
            @Override public void onAdStarted() { }
            @Override public void onError(YouTubePlayer.ErrorReason arg0) { }
            @Override public void onLoaded(String arg0) { }
            @Override public void onLoading() { }
            @Override public void onVideoEnded() { }
            @Override public void onVideoStarted() { }
        });


        youTubePlayer1.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() {
            @Override public void onBuffering(boolean arg0) { }
            @Override public void onPaused() { }
            @Override public void onPlaying() { }
            @Override public void onSeekTo(int arg0) { }
            @Override public void onStopped() { }
        });
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

    }


    public void play(String url)
    {
        if( youTubePlayer1 != null ) {
            youTubePlayer1.cueVideo(url);
        }
    }

    @Override
    public void onPause()
    {
        super.onPause();
        if (youTubePlayer1 != null) {
            if(youTubePlayer1.isPlaying()) {
               // currentPlayerTime = youTubePlayer1.getCurrentTimeMillis();
                youTubePlayer1.pause();
            }
        }
    }



}

1 个答案:

答案 0 :(得分:2)

@Override
public void onPause()
{
    super.onPause();
    if (youTubePlayer1 != null) {
        youTubePlayer1.release(); 
    }
}

您可以将当前视频时间保存在savedInstanceState

但为了更好的解决方案,我建议您在onResume初始化您的播放器,然后在onStop中将其发布。

@Override
protected void onResume() {
    super.onResume();
    playerView.initialize(KEY, this);
}

 @Override
    public void onStop() {
   if (player != null) {
            player.release();
        }
        player = null;
        super.onStop();
    }