Question

I have 25 buttons in my App and 1 Media Player. What I want: When I click first time to play sound. When I click second time to stop sound But what happens: When I click first time it start playing sound, when I click again, it doesn't stop it but instead it's playing it again from beginning. My code:

MediaPlayer mp;
    if(mp!=null)
                        {
                            mp.release();
                            mp=null;
                        }

                        mp = MediaPlayer.create(MainActivity.this, R.raw.s5awesomeguitar);
                        mp.start();

How can I set my code to play Media Player on 1st click , on 2nd click stop sound. On same button Full Code:

gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

        switch (position) {
        case 0:
             if(mp!=null)
             {
                 mp.release();
                 mp=null;
             }
             else {
             mp = MediaPlayer.create(Sadrzaj.this, R.raw.s3djclubmix);
             mp.start();

             }

        break; 
            case 1:
                 if(mp!=null)
                 {
                     mp.release();
                     mp=null;
                 }
                 else {
                 mp = MediaPlayer.create(Sadrzaj.this, R.raw.s16dance);
                 mp.start();

                 }
            break;
            case 2:
                 if(mp!=null)
                 {
                     mp.release();
                     mp=null;
                 }
                 else {
                 mp = MediaPlayer.create(Sadrzaj.this, R.raw.s13ring);
                 mp.start();

                 }

                break;
Was it helpful?

Solution

MediaPlayer is a state machine, here's the Doc .

So, first thing, read the doc, the state diagram says: release() => end.

So if you want to control the same track with different buttons do not call release(); and do set set mp = null; between your cases.

Dhaval has given you the right solution:

if (mp.isPlaying()) { mp.pause() } else { mp.play() }

Also, in order to properly release the player, call first reset() then release() or you'll get a warning saying " the player went awaing with unhandled events.." or something like that.

Please make sure you understand the state diagram and it wiil work like a charm :)

To Sum up:

  1. Read the doc :)

  2. Check the play state of the player

  3. Remove the unnecessary calls to release()

  4. Release the player properly once you're done

Hope this helps!

Edit: Okay so here's what I'd do: (It might not compile, I only have a text editor here so sorry^^)

In your activity(I suppose): Your activity must implement the onPreparedListener

    @Override
onCreate() {
if(mMediaPlayer == null) {
        mMediaPlayer = new MediaPlayer()
        mMediaPlayer.setOnPreparedListener(this); //registers your activity as the onPrepared Listener
    }
    ...
}

//Called back when media is ready to be played
@Override
onPrepared() {
    if(mMediaPlayer != null){
        mMediaPlayer.start();
    }
}


gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {

    switch (position) {
        case 0:
            ..
            break;
        case 1:
         if(mMediaPlayer.isPlaying() {
            mMediaPlayer.pause();
         } else {
             mMediaPlayer.reset();
             mMediaPlayer.setDataSource(Sadrzaj.this, R.raw.s16dance);
             mMediaPlayer.prepareAsync();//call back onPrepared()
             //mMediaPlayer.start();  sorry little mistake here no need to call start(); since it is called in onPrepared()
         }
         break;
         default:
            if(mMediaPlayer != null) {
                mMediaPlayer.reset();
                mMediaPlayer.release();
                mMediaPlayer = null;
            }
            break;
    }
}

//Release the player and de listener
@Override
onDestroy() {
    if(mMediaPlayer != null) {
        mMediaPlayer.setOnPreparedListener(null);
        mMediaPlayer.reset();
        mMediaPlayer.release();
        mMediaPlayer = null
    }
}

OTHER TIPS

You will need to create a service that is bound to the player. The service will be the one that actually plays/changes the track and since they are bound the activity housing the player will be updated.

You did not include much information, but I am going to give you the logic.

if(ButtonClick % 2 == 0){
    //Start playing the music
} else {
    //Stop playing the music
}

or

int i = 1;

                Button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                   if(i == 1){
                         //StartPlaying music .play();
                         i = 2
                   } else if (i==2){
                        //StopPlaying the music .stop();
                        i = 1
                   }
            }
    });

I suggest you using the second method.

Check if the media player is playing and if it is, then change your logic to pause it instead. Otherwise do the opposite.

              Button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
               if(mp.isPlaying()){
                     //StartPlaying music .play();
                    mp.pause();
               } else {
                    mp.start();
               }
        }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top