Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>Can't Android just tell an app "your sound is being paused", cut sound, and display the dialog?

I don't know if it can, but it doesn't. To respond to external audio changes, an app has to use AudioManager.OnAudioFocusChangeListener [1]. You can then define behavior for various cases, like AUDIOFOCUS_LOSS (another app has started playing audio, don't expect it to stop), AUDIOFOCUS_LOSS_TRANSIENT (another app has started playing audio, but it might stop soon so be ready to regain focus) and AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK (another app has started playing audio but it's temporary, so you can just lower your app's volume until you regain focus).

This is all voluntary. By default, an app playing audio won't respond to these signals at all. And how you respond to them is voluntary too - if you decide to just lower your app's volume instead of pausing the audio, even though you didn't hear CAN_DUCK, there's nothing to stop you.

If you want to pause audio when the headset is unplugged, you have register a receiver for the ACTION_AUDIO_BECOMING_NOISY intent. By default audio will continue playing.

>It sounds crazy to me that apps actually have to cooperate on this kind of thing, why do they even get a choice?

Some people might not want their music player to pause every time they get a new text message or email alert.

[1] https://developer.android.com/reference/android/media/AudioM...



> Some people might not want their music player to pause every time they get a new text message or email alert.

Yes, but that shouldn't be the apps choice.


Why not?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: