Nari Kim Shin
Nari Kim Shin

Reputation: 2499

How can I know if the MediaPlayer is in Stopped state?

I am building an app which plays several videos, and I have two different user scenarios :

Scenario 1. While video 'A' is playing, if user clicks next button, then it stops and play the next video 'B'. Scenario 2. Play video 'A', and if it's done, user clicks next button and it plays video 'B'.

For the first scenario, I used mediaPlayer.isPlaying() method to detect if it is in Started state and it works fine. However, if I use the same code for the second scenario, isPlaying() throws IllegalStateException.

Here's my code for playing videos :

private void playVideos(SurfaceHolder holder) {

    try {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDisplay(holder);
        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + video_files[mCounter]);
        mediaPlayer.setDataSource(this, uri);
        mediaPlayer.prepare();
        mediaPlayer.start();
        mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                if (mCounter <= 8) {
                    onVideoCompletion(mediaPlayer);
                } else {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }
            }
        });

    } catch (IOException e) {
    } catch (IllegalArgumentException e) {
    } catch (IllegalStateException e) {
    }
} 

Also, here's my button listener to play next video :

nextBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(mediaPlayer != null) {
                if(mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }
            }
            mCounter += 1;
            if (mCounter <= 8) {
                playVideos(holder);
            }
        }
    });

One way that I tried to hack this issue was using a boolean variable instead of isPlaying() method. For example,

boolean mIsPlaying = false;
...
// in button listener
if(mIsPlaying) {
   mediaPlayer.stop();
   mediaPlayer.release();
}
...
// in playVideos() function
mediaPlayer.start();
mIsPlaying = true;
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

    @Override
    public void onCompletion(MediaPlayer mp) {
        if (mCounter <= 8) {
            onVideoCompletion(mediaPlayer);
        } else {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }
});

That works for my both of scenario, but I'm not sure if it's the correct way to do it. Isn't there any way to detect whether mediaPlayer is in Stopped state?

Upvotes: 0

Views: 1407

Answers (1)

Stanete
Stanete

Reputation: 691

I took a look at Google's Documentation which you can find here. You can only know if the player isPlaying(); or isLooping(); ... So no, there is not an "easy" or "short" way to achieve what you want. Hope it helped.

Upvotes: 1

Related Questions