Play and stop button doesn't work together

Aroo :

I have 2 buttons in my layout. 1. Play/Pause 2. Stop Play and pause button works great but when stop button doesn't work. I don't actually know where the problem begins.

Here is my code

            case R.id.playBtn:
            if (mediaPlaying){
                mediaPlaying = false;
                mPlayer.pause();
                playedLength = mPlayer.getCurrentPosition();
                playButton.setBackgroundResource(R.drawable.play);
            }else{
                mediaPlaying = true;
                if(mPlayer == null){
                    // create the media player
                    mPlayer = new MediaPlayer();
                    mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            public void onCompletion(MediaPlayer mp) {
                                mediaPlaying = false;
                                playedLength = 0;
                                playButton.setBackgroundResource(R.drawable.play);
                            }
                        });
                    try {
                        afd = getAssets().openFd(""+AUDIO+"");
                        mPlayer.setDataSource(afd.getFileDescriptor());
                        mPlayer.prepare();
                        mPlayer.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else
                // resume playing
                mPlayer.seekTo(playedLength);
                mPlayer.start();
                playButton.setBackgroundResource(R.drawable.pause);
            }

            break;

        case R.id.stopBtn:
            if (mediaPlaying){
                mediaPlaying = false;
                if (mPlayer != null){
                    mPlayer.stop();
                    mPlayer = null;
                    playButton.setBackgroundResource(R.drawable.play);
                    }
            }else{
                mediaPlaying = true;
                Toast.makeText(getApplicationContext(), "Added to favorites.", Toast.LENGTH_SHORT).show();
                if (mPlayer != null){
                    mPlayer.pause();
                    mPlayer.seekTo(0);
                    }
            }
            break;

I want to do this When click on stop button, completely stop player whether it is playing or paused. While playing if I click on stop, the player restarts but when it is paused and click on stop it doesn't restart. Any help is appreciated.

Update

case R.id.playBtn:
            if (state == PlayerState.PLAYING){
                mPlayer.pause();
                playedLength = mPlayer.getCurrentPosition();
                playButton.setBackgroundResource(R.drawable.play);
            }else if (state == PlayerState.PLAYING){

                if(mPlayer == null){
                    // create the media player
                    mPlayer = new MediaPlayer();
                    mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            public void onCompletion(MediaPlayer mp) {
                                mediaPlaying = false;
                                playedLength = 0;
                                playButton.setBackgroundResource(R.drawable.play);
                            }
                        });
                    try {
                        afd = getAssets().openFd(""+AUDIO+"");
                        mPlayer.setDataSource(afd.getFileDescriptor());
                        mPlayer.prepare();
                        mPlayer.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else
                // resume playing
                mPlayer.seekTo(playedLength);
                mPlayer.start();
                playButton.setBackgroundResource(R.drawable.pause);
            }

            break;

        case R.id.stopBtn:
            if (state == PlayerState.STOPPED){
                if (mPlayer != null){
                    mPlayer.stop();
                    mPlayer = null;
                    playButton.setBackgroundResource(R.drawable.play);
                    }
            }else if (state == PlayerState.PAUSED){
                Toast.makeText(getApplicationContext(), "Added to favorites.", Toast.LENGTH_SHORT).show();
                if (mPlayer != null){
                    mPlayer.pause();
                    mPlayer.seekTo(0);
                    }
            }
            break;
Jocke :

The reason that you experience different behavior if that the boolean mediaPlaying will have different values when the stop button is clicked. It will only stop the player is the value is true, and if you hit stop when the player is paused it will be false so you code will not stop the player.

I suggest that you add a PlayerState enum for e.g. (PLAYING, STOPPED, PAUSED) instead of using booleans. It will then be easier for you to know what state the player is in, and what action to take.

In in code

public enum PlayerState {
    PLAYING,
    STOPPED,
    PAUSED
}

Then you create the variable and set the initial state (same as you do for mediaPlaying (and in the same place in you application))

private PlayerState state = PlayerState.STOPPED;

and then in you code you replace the places where you manipulate mediaPlaying and replace the if(mediaPlaying) with if-statements like

if (state == PlayerState.PLAYING) { ...

Once this is done, you no longer need the boolean mediaPlaying. And you have better details knowledge of the media player state in you applications.

Some kind of state code, hard since I don't have all the code. In each case check the state transition and then set the state (that is missing in you code)

        case R.id.playBtn:
            if (state == PlayerState.PLAYING){
                // logic here for going from playing to paused
                state = PlayerState.PAUSED;
            } else if (state != PlayerState.PLAYING){
                // logic from non playing to playing
                state = PlayerState.PLAYING;
            }
            break;
        case R.id.stopBtn:
            if (state == PlayerState.PLAYING){
                // logic if playing
            } else if (state == PlayerState.PAUSED){
                // logic if paused
            }
            state = PlayerState.STOPPED;
            break;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=349323&siteId=1