Monday 13 January 2014

Check if music is already playing on Android from within an Activity

Problem:

How do I check if music is already playing on an Android device from within an Activity?

Solution:

AudioManager aMan = (AudioManager)this.getSystemService(
                                         Context.AUDIO_SERVICE);
if(aMan.isMusicActive()) {
  // do stuff here if music is playing
}

Notes:

This check can be useful if your app needs to play music. This is because there are instances where attempting to play music using MediaPlayer while music is already playing will corrupt an audio stream on an Android device.

References:

2 comments: