Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : youtube player has been released

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
}

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

like image 285
Mickey Tin Avatar asked Jan 24 '14 12:01

Mickey Tin


People also ask

How do you release YouTube on Android?

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.)

What is YouTube player API?

The YouTube Android Player API enables you to incorporate video playback functionality into your Android applications. The API defines methods for loading and playing YouTube videos (and playlists) and for customizing and controlling the video playback experience.

What is YouTube player?

Overview. A YouTubePlayer provides methods for loading, playing and controlling YouTube video playback. Get an instance of this class by calling initialize on a YouTubePlayer. Provider such as YouTubePlayerFragment or YouTubePlayerView .


2 Answers

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();
}
like image 182
Ganesh Mohan Avatar answered Sep 19 '22 22:09

Ganesh Mohan


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

Edit:

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

like image 41
Vijay C Avatar answered Sep 21 '22 22:09

Vijay C