【发布时间】:2015-09-01 21:33:57
【问题描述】:
即使静音模式已开启,我也会尝试播放通知声音
Uri uri = Uri.parse(alarmSound);
notification.setSound(uri);
AudioManager mobileMode = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int previousNotificationVolume = mobileMode.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
if (ignoreSilent) {
mobileMode.setStreamVolume(AudioManager.STREAM_NOTIFICATION, mobileMode.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);
}
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(UNIQUE_ID, n);
try {
// to delay make a space to finish play sound before return back to original stat.
Thread.sleep(2000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
mobileMode.setStreamVolume(AudioManager.STREAM_NOTIFICATION, previousNotificationVolume, 0);
这应该是用户启用或禁用的选项,我正在尝试上面的代码,但事情进展不顺利,“有时”听到声音,手机返回 振动 模式而不是沉默,我要处理棒棒糖盒:
- 在静音模式下,定时或无限期。
- 在优先模式下,定时或无限期。
在另一个世界,我想要一些东西来保存完整的统计数据并按原样返回。
或者,因为我知道如何用媒体播放器播放声音,如何获取手机的状态,如果它是无声的,则将声音作为媒体播放,媒体声音最大,以下我可以如何播放声音使用媒体播放器:
public static void playSound(Context context, Uri alert) {
MediaPlayer mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(context, alert);
final AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}
然后像这样(但我想知道手机是否处于静音或振动或优先模式):
if (ignoreSilent) {
CoreServices.playSound(context, uri);
} else {
// else just follow normal behavior
notification.setSound(uri);
}
最后,我更喜欢通过切换状态来解决,至少我会知道android在这部分是如何处理的,并且使用API 14+。
【问题讨论】:
-
在静音模式(音量为零)期间,警报是唯一会响起的声音。但是,如果用户将手机设置为“无”中断模式,即使是闹钟也不会响起。
-
@Populus 在这种情况下它的用户需要,但我想为其他情况解决问题。
标签: android android-notifications