[英]Playing sound even when phone is in silent/ DONOT disturb mode
I have a use case where i want to play sound even when the phone is in silent mode on API level > 23. After a bit of googling i found that i should explicitly ask the permission from user to do so. 我有一个用例,即使手机处于API级别> 23的静音模式下,我也要播放声音。经过一番谷歌搜索后,我发现我应该明确地征询用户的许可。 Now the permission is all set using following code. 现在,使用以下代码设置所有权限。
private void requestForDoNotDisturbPermissionOrSetDoNotDisturbForApi23AndUp() {
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
// Open Setting screen to ask for permisssion
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivityForResult( intent, ON_DO_NOT_DISTURB_CALLBACK_CODE );
startActivity(intent);
}
The sound is also played in silent mode using media player after adjusting the volume. 调整音量后,还可以使用媒体播放器以静音模式播放声音。
mAudioManager.setStreamVolume(stream, deviceLocalVolume,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
Once played the sound is reset to original sound volume. 播放后,声音将重设为原始音量。 But i see that the phone is no longer in donotdisturb mode. 但是我看到电话不再处于“ 请勿打扰”模式。 Why so? 为什么这样?
EDIT: Detailed code: 编辑:详细代码:
void playSound(int stream) {
mAudioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
int deviceLocalVolume = getDeviceVolume(volume,
mAudioManager.getStreamMaxVolume(stream));
Log.d(TAG,
"device max volume = "
+ mAudioManager.getStreamMaxVolume(stream)
+ " for streamType " + stream);
Log.d(TAG, "playing sound " + uri.toString()
+ " with device local volume " + deviceLocalVolume);
oldVolume = mAudioManager.getStreamVolume(stream);
Log.d(Constants.APP_TAG, "setting device local volume to "
+ deviceLocalVolume);
mAudioManager.setStreamVolume(stream, deviceLocalVolume,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
final MediaPlayer mediaPlayer = new MediaPlayer();
golbalMMediaPlayer = mediaPlayer;
try {
final OnPreparedListener OnPreparedListener = new OnPreparedListener() {
@Override
public void onPrepared(final MediaPlayer mp) {
Log.d(TAG, "OnPreparedListener prepared.Actually playing music now");
mp.start();
countDownTimer = new CountDownTimer(maxTime * 1000,
tickTime * 1000) {
@Override
public void onTick(long millisUntilFinished) {
Log.d(TAG, "tick while playing sound ");
}
@Override
public void onFinish() {
Log.d(TAG, "timer finished");
resetVolume();
stopPlaying();
}
};
countDownTimer.start();
}
};
mediaPlayer.setDataSource(context.getApplicationContext(), uri);
mediaPlayer.setAudioStreamType(stream);
mediaPlayer.setLooping(false);
mediaPlayer.setOnPreparedListener(OnPreparedListener);
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.d(TAG, "Done playing music now");
resetVolume();
Log.d(TAG, "releasing mediaplayer now");
try {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.release();
}
} catch (Exception ex) {
Log.e(Constants.APP_TAG,
"error on oncompletion listener", ex);
}
}
});
mediaPlayer.prepareAsync();
}
private static int getDeviceVolume(int currentVolume, int deviceMaxVolume) {
return (int) Math.ceil((deviceMaxVolume * currentVolume) / 100);
}
private static void resetVolume() {
try {
Log.d(Constants.APP_TAG, "reseting volume to " + oldVolume
+ " for stream = " + stream);
mAudioManager.setStreamVolume(stream, oldVolume,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
} catch (Exception ex) {
Log.e("error", "error", ex);
}
}
you have to switch the DnD back on: 您必须重新打开DnD:
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.setNotificationPolicy(NotificationManager.INTERRUPTION_FILTER_NONE);
see INTERRUPTION_FILTER_NONE : 参见INTERRUPTION_FILTER_NONE :
All notifications are suppressed except those that match the priority criteria. 除符合优先级条件的那些通知外,所有通知均被取消。
Some audio streams are muted. 一些音频流被静音。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.