Dave Feldman
Dave Feldman

Reputation: 194

Honoring system mute state with MediaPlayer

Our app is using MediaPlayer to play sound effects. We'd like those not to play when the user has silenced the phone (e.g., set it to vibrate or silent and not plugged in headphones).

By default, it doesn't honor the system state: the sound plays regardless. I can use AudioManager.getRingerMode() to stop the sound playing, but (a) it feels like a hack, and (b) it doesn't handle the headphone case -- if I'm listening to music on headphones but have silenced the ringer, I'd like the sound to play.

Is there a way to do this?

Upvotes: 0

Views: 113

Answers (1)

jonstaff
jonstaff

Reputation: 2672

It seems to me that you'll have to use a combination of logic with getRingerMode() and isWiredHeadsetOn() to pause or mute the audio accordingly.

NOTE: isWiredHeadsetOn() was deprecated for some reason in API 14, but remains the only way to check whether or not headphones are connected. The docs state:

Use only to check is a headset is connected or not.

Read about that here.

So yes, there is a way to do this, though I don't know if it's precisely what you were looking for.

Upvotes: 1

Related Questions