Android 使用铃声管理器中断媒体播放器
Android interrupt media player with ringtone manager
我之前在 android studio 中使用过 ringtonemanager,它似乎降低了在不同应用程序中播放的任何音乐的音量来播放我尝试播放的警报,然后一旦我的警报完成然后背景音乐会恢复到正常音量(默认 alarm/notification 会这样做)但是现在大约一年后我试图再次实现这一点但是我的警报无法通过 [=22= 中播放的音乐听到] 播放音乐。
这是现在需要额外参数才能像以前一样运行的更改吗?
我正在使用:
Uri notification =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
myRM = RingtoneManager.getRingtone(this.getContext(), notification);
myRM.play();
非常感谢
Handling Changes in Audio Output 似乎符合您的需要。
简而言之,您需要在开始播放之前请求音频焦点。
...
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.requestAudioFocus(null, mStreamType, AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
mRingtone.play();
AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
Used to indicate a temporary request of audio focus, anticipated to
last a short amount of time, and where it is acceptable for other
audio applications to keep playing after having lowered their output
level (also referred to as "ducking").
完成后记得释放音频焦点..
if (mRingtone != null && mRingtone.isPlaying()) {
mRingtone.stop();
}
mRingtone = null;
if (mAudioManager != null) {
mAudioManager.abandonAudioFocus(null);
}
我之前在 android studio 中使用过 ringtonemanager,它似乎降低了在不同应用程序中播放的任何音乐的音量来播放我尝试播放的警报,然后一旦我的警报完成然后背景音乐会恢复到正常音量(默认 alarm/notification 会这样做)但是现在大约一年后我试图再次实现这一点但是我的警报无法通过 [=22= 中播放的音乐听到] 播放音乐。
这是现在需要额外参数才能像以前一样运行的更改吗?
我正在使用:
Uri notification =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
myRM = RingtoneManager.getRingtone(this.getContext(), notification);
myRM.play();
非常感谢
Handling Changes in Audio Output 似乎符合您的需要。
简而言之,您需要在开始播放之前请求音频焦点。
...
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.requestAudioFocus(null, mStreamType, AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
mRingtone.play();
AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
Used to indicate a temporary request of audio focus, anticipated to last a short amount of time, and where it is acceptable for other audio applications to keep playing after having lowered their output level (also referred to as "ducking").
完成后记得释放音频焦点..
if (mRingtone != null && mRingtone.isPlaying()) {
mRingtone.stop();
}
mRingtone = null;
if (mAudioManager != null) {
mAudioManager.abandonAudioFocus(null);
}