Android中活动简历的Youtube播放器崩溃异常

时间:2017-08-25 07:24:50

标签: android youtube crash onresume

我添加了youtube播放器,用于流式传输视频。当应用处于后台时,视频会暂停简历就在前景中。 但在某些情况下它会崩溃,什么时候恢复状态。 crashalytics的PFB截图: enter image description here

它显示了活动恢复中发现的死对象。

这是我的Youtube播放器活动代码:

   @Override
protected void onDestroy() {
    Log.d("#ScreenONOFF","onDestroy" );
    YouTubeShareClass.getInstance().showInterestialAd(this,"Video Player");

    if (youTubePlayer != null) {
        youTubePlayer.release();
        youTubePlayer = null;
    }
    super.onDestroy();
    if (mReceiver != null) {
        unregisterReceiver(mReceiver);
    }
}

@Override
public void onResume()
{
    super.onResume();
    if (youTubePlayer != null && videoID != null) {
        youTubePlayer.loadVideo(videoID,currentPlayerTime);
        youTubePlayer.play();
    }
}

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

@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
    if (videoID != null) {
        youTubePlayer = player;
        if (youTubePlayer != null && mReceiver != null) {
            if (wasRestored) {
                player.play();
            } else {
                try {
                    player.loadVideo(videoID,0);
                } catch (IllegalStateException e) {
                    youTubeView.initialize(YouTubeConstants.youtube_API_key, this);
                }
            }
            mReceiver.player = player;
            player.setFullscreen(true);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。虽然我找到了问题的谷歌跟踪器(https://issuetracker.google.com/issues/35172585),但重现错误很复杂。

引用此回答(android.os.DeadObjectException at com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer.x)Google对此错误有所说明:

  

public static final YouTubePlayer.ErrorReason UNEXPECTED_SERVICE_DISCONNECTION

     

播放已被取消,播放器因YouTube API服务意外断开而被释放。对此播放器实例的任何进一步调用都将导致错误,必须创建新的播放器实例才能重新启用播放。

我无法100%验证此解决方案是否有效,因为我无法重现错误,但这可能有所帮助:

if(ytPlayer != null) {
    try {
        ytPlayer.loadVideo(videoId);
    } catch (IllegalStateException e) {
        initialize(API_KEY, this);
    }
}