I want to replace the notification sound if my app is running. Therefore I have used the AudioManager
to get the Request. Also I have used it to mute the Notification
. To play the sound I use the MediaPlayer
The Code looks like this:
final AudioMan
ager am = (AudioManager) this
.getSystemService(AUDIO_SERVICE);
int result = am.requestAudioFocus(null,
AudioManager.STREAM_NOTIFICATION,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
am.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
MediaPlayer mp = MediaPlayer.create(this, uri);
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.e("release", "release!");
mp.release();
}
mp.start();
});
am.abandonAudioFocus(new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
// Lower the volume
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// Raise it back to normal
}
}
});
But after these. I can't set the "normale" Volume Back. Normale it looks like that: https://i.stack.imgur.com/AJq3n.png but now it looks like that: https://i.stack.imgur.com/gESlV.png
What is wrong?