Android - using AudioManager to play/pause music?
-
27-10-2019 - |
Question
My question is how can I use the AudioManager class to 1) see if a music service is open, and 2) if it's paused, play it, if it's playing, pause it..?
So far all I've really done is initialize an instance of it:
AudioManager AM = (AudioManager) getSystemService(AUDIO_SERVICE);
if (AM.isMusicActive()) {
//how do I pause whatever music player is active?
}
Similarly, if a music player is open and paused, how do I play it? I've looked into the MediaPlayer class but I'm not sure how to use it with AudioManager (or if I even want to). While I'm asking questions, is there manifest permissions I need to worry about?
Thanks guys!
Solution
2) if it's paused, play it, if it's playing, pause it..?
There are no APIs to do this. You can request the audio focus if you need to temporarily take over audio playback (e.g., to play a ringtone associated with an alarm).
Similarly, if a music player is open and paused, how do I play it?
You don't.
I've looked into the MediaPlayer class but I'm not sure how to use it with AudioManager (or if I even want to).
MediaPlayer
is for playing media in your own application.
OTHER TIPS
Tasker does it. At least pausing playback. It doesn't look like it can start it again.