Playing and Pausing Media Player on same button using only 1 Media Player instance?

三世轮回 提交于 2019-12-13 04:22:45

问题


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;

回答1:


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
    }
}



回答2:


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.




回答3:


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.




回答4:


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




回答5:


              Button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
               if(mp.isPlaying()){
                     //StartPlaying music .play();
                    mp.pause();
               } else {
                    mp.start();
               }
        }
});


来源:https://stackoverflow.com/questions/22352263/playing-and-pausing-media-player-on-same-button-using-only-1-media-player-instan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!