暂停/恢复的MediaPlayer的通知声音声音、通知、MediaPlayer

2023-09-05 01:25:43 作者:冷色系里的一抹暖色调

这很容易发现,当电话进来(通过phoneStateListener),但对于其他的通知声音,如电子邮件或短信?

It's easy to detect when phone calls come in (via phoneStateListener), but what about other notification sounds, such as email or sms?

在某些设备上,这些提示音哑,但不暂停所有正在运行的媒体播放器实例,这是烦人的用户。理想情况下,我想侦听播放的声音通知,暂停播放的时间,然后重新开始播放之后。

On some devices, these notification sounds mute but don't pause any currently running mediaPlayer instances, which is annoying for the user. Ideally, I'd like to listen for notifications that play sound, pause playback for the duration, and then resume playback afterwards.

推荐答案

您可以得到通知时,另一个应用程序要通过注册一个回调来播放音频 AudioManager.OnAudioFocusChangeListener 的(这也将处理来电的情况下)。具体来说,你可以看看 AUDIOFOCUS_GAIN AUDIOFOCUS_LOSS AUDIOFOCUS_TRANSIENT_LOSS 。 Android的音乐播放器源具有良好的这个例子。

You can get notified when another app wants to play audio by registering a callback on AudioManager.OnAudioFocusChangeListener (this would also handle the case of an incoming call). Specifically, you can look for AUDIOFOCUS_GAIN, AUDIOFOCUS_LOSS, and AUDIOFOCUS_TRANSIENT_LOSS. The Android music player source has a good example of this.