In my project, I am playing music file in android media player by using the following code
MediaPlayer mPlayer = MediaPlayer.create(MyActivity.this, R.raw.myfile); mPlayer.start();
the above is coded in the onclick of the play button. I want to pause the playback by clicking the same button again.ie) single button for play/pause. How shall i do this.
There is no API to check if MediaPlayer is paused or not. So please use any Boolean variable to check and toggle it when you paused using any button .
You'll need to either stop playback and kill the app that started them; swipe left on the notification (which doesn't always work); or stop playback, long-press the expanded controls, and select "Close this media session," which is a little awkward — why not just an X in the corner?
You could use simple if-check to handle the pausing. Try this:
if(mPlayer.isPlaying()){ mPlayer.pause(); } else { mPlayer.start(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With