0

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?

StefMa
  • 3,344
  • 4
  • 27
  • 48
  • Sorry, I do not get it : what do you do in `// Raise it back to normal` ? Is it called? – Poutrathor Nov 19 '13 at 17:57
  • No.. its never called. But think there is the error. Because abandonAudioFocus is to "deFocus"... There http://stackoverflow.com/a/6577803/1231245 is the description what abandonAudioFocus does... – StefMa Nov 20 '13 at 09:26
  • hope [THIS](http://stackoverflow.com/q/15809399/1289716) will help you. – MAC Nov 20 '13 at 10:14
  • Din't help me. Because I don't start or create a notification...The only thing is it to mute and play a sound... Or override... – StefMa Nov 20 '13 at 15:44

0 Answers0