我正在构建一个闹钟应用程序,当手机静音时,我希望它播放闹钟,但保持手机模式静音。我现在的闹钟应用程序(defualt samsung / android stock one)使手机保持静音模式,但你仍然听到警报。
我尝试设置它,以便检查音频是静音还是振动,设置声音然后重置音频。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//get audiomanager and set the volume
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int oldVolume = 0;
int maxVolume = 0;
boolean silent = false;
SoundMode soundMode = null;
if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT
|| audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
soundMode = soundMode.SILENT;
} else if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
soundMode = soundMode.VIBRATE;
}
oldVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
maxVolume = maxVolume/2;
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolume, AudioManager.FLAG_ALLOW_RINGER_MODES);
silent = true;
Log.i(TAG, "silent if statement " + soundMode);
}
// audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolume / 2, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
Uri alarmTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
ringtoneAlarm = RingtoneManager.getRingtone(getApplicationContext(), alarmTone);
ringtoneAlarm.play();
if (silent) {
if (soundMode == SoundMode.SILENT) {
Log.i(TAG, "silent mode");
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} else if (soundMode == SoundMode.VIBRATE) {
Log.i(TAG, "vibrate mote");
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
}
return START_NOT_STICKY;
}
我已经达到手机知道它何时处于静音或振动模式的程度,但它不会播放铃声,它只是振动或不发出噪音。我如何制作它以便无论如何播放铃声,但保持音频状态如何?
答案 0 :(得分:1)
所以在寻找其他一些问题之后Troubles play sound in silent mode on Android
为我解决了。
我必须使用
RingtoneManager.getRingtone()。setStreamType(AudioManager.STREAM_ALARM),以静音模式播放闹钟。