安卓:YouTube播放器已被释放已被、播放器、YouTube

由网友(一步之错,步步错)分享简介:我得到这个错误致命异常:java.lang.IllegalStateException这YouTubePlayer已经发布,而发布()不叫explicitly.Here是一块code其中发生崩溃时:I'm getting this error Fatal Exception: java.lang.IllegalSt...

我得到这个错误致命异常:java.lang.IllegalStateException 这YouTubePlayer已经发布,而发布()不叫explicitly.Here是一块code其中发生崩溃时:

I'm getting this error Fatal Exception: java.lang.IllegalStateException This YouTubePlayer has been released , but release() wasn't called explicitly.Here is the piece of code where crash occurs :

if(youtubePlayer != null){
 time = youtubePlayer.getCurrentTimeMillis();//exception may occur
}

是可以检查 youtubePlayer 发布?任何回调?谢谢你。

is it possible to check that youtubePlayer was released? Any callback ? Thanks.

推荐答案

在大多数情况下code在Youtube的SDK进行模糊处理,这使得它真的很难调试。这的事实,没有任何直接的方法来检查YoutubePlayer已被释放或不也没有帮助。

Most of the code in the Youtube SDK is obfuscated which makes it really hard to debug. And the fact that there isn't any direct method to check if the YoutubePlayer has been released or not doesn't help either.

说了这么多,我认为制作YoutubePlayer空(在的onStop()),似乎更多的是破解的不是一个妥善的解决办法给我。您应该释放YoutubePlayer中的onDestroy(),不要任何地方手动分配为空。一个简单的方法来检查YoutubePlayer已被释放与否是把你的电话(如youtubePlayer.loadVideo(),cueVideo(),getCurrentTimeMillis()等)在try catch块,赶上了IllegalStateException异常例外。

Having said that I think making YoutubePlayer null (in onStop()) seems more of a hack than a proper solution to me. You should release the YoutubePlayer in onDestroy() and don't manually assign it as null anywhere. One simple approach to check if the YoutubePlayer has been released or not is put your calls (like youtubePlayer.loadVideo(), cueVideo(), getCurrentTimeMillis() etc.) in a try catch block and catch the IllegalStateException exception.

根据上的错误的Youtube SDK文档:

公共静态最终YouTubePlayer.ErrorReason   UNEXPECTED_SERVICE_DISCONNECTION

public static final YouTubePlayer.ErrorReason UNEXPECTED_SERVICE_DISCONNECTION

播放已经被取消和玩家已被释放,由于一个   从YouTube的API服务突然中断。任何进一步的   调用此播放器实例会导致错误,一个新的球员   实例必须创建以重新启用播放功能。

Playback has been canceled and the player has been released due to an unexpected disconnection from the YouTube API service. Any further calls to this player instance will result in errors, a new player instance must be created to re-enable playback.

因此创造了YoutubePlayer的新实例只需要调用catch块的initialize()方法。

例如:

public void setVideoId(final String videoId) {
    if (videoId != null && !videoId.equals(this.videoId)) {
        this.videoId = videoId;
        if (youtubePlayer != null) {
            try {
                youtubePlayer.loadVideo(videoId);
            } catch (IllegalStateException e) {
                initialize(API_KEY, this);
            }
        }
    }
}

@Override
public void onDestroy() {   
    if (youtubePlayer != null) {
        youtubePlayer.release();
    }
    super.onDestroy();
}
阅读全文

相关推荐

最新文章