Mishil D.
Mishil D.

Reputation: 107

Why does my MediaPlayer stop working if I "interrupt" it?

I want my button to be "spammable". This means that if I tap on the button repeatedly the MediaPlayer starts all over again.

firstButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (firstTextView.getText().equals("Hello world!")) {

                firstTextView.setText("You clicked!");


            } else {

                firstTextView.setText("Hello world!");

            }

            if (mediaPlayer.isPlaying()) {

                mediaPlayer.stop();

            }

            mediaPlayer.start();

        }
    });

When I interrupt the MediaPlayer when it is playing, it stops and never starts again. Why?

EDIT: The problem is that I called stop(). Thanks for pointing that out.

Upvotes: 2

Views: 307

Answers (2)

Nikola Despotoski
Nikola Despotoski

Reputation: 50578

Seems you are stopping the player because you are calling mediaPlayer.stop() this makes the MediaPlayer state to go in Stopped state. It will continue to play again when you call prepare() or prepareAsync() and has its preparation callback fired to start the playing media.

Upvotes: 1

A--C
A--C

Reputation: 36449

As per the documentation, you need to re-prepare the MediaPlayer (emphasis mine):

Once in the Stopped state, playback cannot be started until prepare() or prepareAsync() are called to set the MediaPlayer object to the Prepared state again.

Upvotes: 2

Related Questions