I'm trying to play notification sound even if silent mode is on
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);
This should be option that user enable or disable, I'm trying the code above but the things not going well, the sound heard "sometimes" and the phone return to the Vibration mode instead of silent, I want to handle lollipop case:
In another world, I want something to save full stat and return it as it was.
Or, since I know how to play sound with media player, way to get the stat of phone, and if it is silent, make sound playing as a media, with max sound of media, the following how I can play sound with MediaPlayer :
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());
}
}
And then like this (but I want to know if phone in silent or vibration or priority mode) :
if (ignoreSilent) {
CoreServices.playSound(context, uri);
} else {
// else just follow normal behavior
notification.setSound(uri);
}
Finally, I prefer to solve by switching status, at least I will know how android deal things in this part, and working with API 14+.
答案 0 :(得分:0)
public void playSound(){
final MediaPlayer mMediaPlayer;
Uri notification = null;
try {
notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(getApplicationContext(), notification);
// mMediaPlayer = MediaPlayer.create(ctx, notification);
final AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
try {
mMediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
// mMediaPlayer.start();
mMediaPlayer.setLooping(false);
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
/* if (mMediaPlayer != null) {
mMediaPlayer.stop();
}*/
mMediaPlayer.seekTo(0);
mMediaPlayer.start();
}
});
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
}
});
}catch (Exception e){
e.printStackTrace();
}
}