Android : youtube player has been released

前端 未结 4 1468
傲寒
傲寒 2020-12-15 21:42

I\'m getting this error Fatal Exception: java.lang.IllegalStateException This YouTubePlayer has been released , but release() wasn\'t called explic

相关标签:
4条回答
  • 2020-12-15 21:56

    This is how I managed to get rid of this exception. In my case it was thrown on onSaveInstanceState where I tried to save current player position using the same piece of code:

    if(youtubePlayer != null){ time = youtubePlayer.getCurrentTimeMillis(); }
    

    and upon successfull player initialization in onInitializationSuccess I continued playing video using the time value assigned in onCreate from Bundle.

    But it turned out that such approach is unnecessary. To avoid the exception throw I just added android:configChanges="orientation|screenSize" attribute to my player activity in manifest. This makes the system handle orientation changes by itself, and adding time parameter to cueing the video becomes redundant. This is what checking the wasRestored flag is made for in onInitializationSuccess.

    Here's the changes summary:

    AndroidManifest.xml:

    <activity android:name=".VideoPlayerActivity"
    android:configChanges="orientation|screenSize"/>
    

    VideoPlayerActivity.java:

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
            this.mPlayer = player;
            if (!wasRestored) {
                mPlayer.loadVideo(mVideoId);
            }
        }
    
    0 讨论(0)
  • 2020-12-15 22:06

    I fixed this issue by making videoPlayer null in onDestroy of Activity.

    Edit:

    Tried in onStop and it works fine.
    @MickeyTin, Thanks..

    0 讨论(0)
  • 2020-12-15 22:08

    The type of variable returned by youtubePlayer.getCurrentTimeMillis(); is a int.
    This should work:

    if(youtubePlayer != null){
     int millis = youtubePlayer.getCurrentTimeMillis();//exception may occur
    }
    
    0 讨论(0)
  • 2020-12-15 22:10

    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.

    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.

    According to the Youtube SDK documentation on errors:

    public static final YouTubePlayer.ErrorReason UNEXPECTED_SERVICE_DISCONNECTION

    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.

    So to create a new instance of the YoutubePlayer just call the initialize() method in the catch block.

    Example:

    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();
    }
    
    0 讨论(0)
提交回复
热议问题