简体   繁体   中英

Get the R.id of an audio file that is currently playing in MediaPlayer on Android

I have an app where I display some buttons and depending on the button you click, a MediaPlayer plays one sound or another.

Everything is working fine, but now I'd like my app to stop playing the sound of a button, if the user clicks it twice on a row.

Let's say I have three buttons (BT1, BT2 and BT3). Right now, if you click BT1 and then BT2, BT1 stops sounding and BT2 starts. But if you click the same button twice, it stops playing and starts again, which is obvious.

What I'd like to do, is that if you click BT1 and then BT1 again, the first click starts the sound of BT1 and the second one stops it, so the user can stop one sound by clicking the button again. The problem is that I don't know if there's a MediaPlayer method to get the current id that is playing.

Any help would be appreciated. Thanks a lot.

As per my understanding you wants to play and pause music by clicking the same button. You can check the media-player state by below code:

mediaplayer.isPlaying()

And as per its state you can use play and stop/pause functions in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM