Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Media Player play/pause Button

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.

like image 273
Dray Avatar asked Feb 27 '12 06:02

Dray


People also ask

How do I know if my Android MediaPlayer is paused?

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 .

How do I close Android MediaPlayer?

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?


1 Answers

You could use simple if-check to handle the pausing. Try this:

if(mPlayer.isPlaying()){     mPlayer.pause(); } else {     mPlayer.start(); } 
like image 200
Ruuhkis Avatar answered Nov 09 '22 08:11

Ruuhkis