静音通知并播放自己的声音

时间:2013-11-19 17:32:18

标签: android notifications android-mediaplayer android-audiomanager

如果我的应用正在运行,我想替换通知声音。因此,我使用AudioManager来获取请求。我也用它来静音Notification。要播放声音我使用MediaPlayer 该代码如下所示: 最终的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
        }
    }
});

但在这之后。我不能设置“normale”音量回来。 Normale看起来像那样:http://i.imgur.com/pZwINHz.png但现在它看起来像那样:http://i.imgur.com/fqR6yRA.png

有什么问题?

0 个答案:

没有答案