【问题标题】:Notification with sound although phone is muted尽管手机静音但有声音通知
【发布时间】:2014-10-14 05:40:19
【问题描述】:

我正在使用 Android 的 NotificationManager 创建通知。

是否可以“覆盖”手机的音量(静音)设置,从而始终播放通知的声音?

我需要这个的原因如下: 通知非常重要,仅靠振动可能还不够。必须提醒用户。因此,即使手机静音或音量很低,也会播放声音。

【问题讨论】:

    标签: android notifications android-notifications


    【解决方案1】:

    是的,这是可能的,

    MediaPlayer mMediaPlayer;
    Uri notification = null;
    notification = RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_ALARM);
    
    mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(ctx, notification);
            // mMediaPlayer = MediaPlayer.create(ctx, notification);
            final AudioManager audioManager = (AudioManager) ctx
                    .getSystemService(Context.AUDIO_SERVICE);
    
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
    
            mMediaPlayer.prepare();
            // mMediaPlayer.start();
            mMediaPlayer.setLooping(true);
            mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
                public void onPrepared(MediaPlayer arg0) {
                    mMediaPlayer.seekTo(0);
                    mMediaPlayer.start();
    
                }
    
            });
    

    【讨论】:

    • 这段代码如何改变手机的音量/静音模式?它是隐式完成的吗?还是我只是瞎了眼? ;-)
    • mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);此行更改音频类型
    【解决方案2】:

    您可以像这样将 RINGING 模式从静音更改为正常

    AudioManager mobilemode = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            mobilemode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    
    
            // Turn on all sound
    
            // turn on sound, enable notifications
            mobilemode.setStreamMute(AudioManager.STREAM_SYSTEM, false);
            //notifications
            mobilemode.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
            //alarm
            mobilemode.setStreamMute(AudioManager.STREAM_ALARM, false);
            //ringer
            mobilemode.setStreamMute(AudioManager.STREAM_RING, false);
            //media
            mobilemode.setStreamMute(AudioManager.STREAM_MUSIC, false);
    
    
            // Turn off all sound
    
            // turn off sound, disable notifications
            mobilemode.setStreamMute(AudioManager.STREAM_SYSTEM, true);
            //notifications
            mobilemode.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
            //alarm
            mobilemode.setStreamMute(AudioManager.STREAM_ALARM, true);
            //ringer
            mobilemode.setStreamMute(AudioManager.STREAM_RING, true);
            //media
            mobilemode.setStreamMute(AudioManager.STREAM_MUSIC, true);
    

    对于您的情况,您可以尝试这样的方法

    int previousNotificationVolume =mobilemode.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
    
            mobilemode.setStreamVolume(AudioManager.STREAM_NOTIFICATION,mobilemode.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);
    
            // Play notification sound
    
    
    
            // Set notification sound to its previous 
    
            mobilemode.setStreamVolume(AudioManager.STREAM_NOTIFICATION,previousNotificationVolume, 0);
    

    【讨论】:

    • 看起来很有希望。播放通知声音后,是否有一种时尚的方法可以将设置恢复到初始值?例如。不使用固定计时器之类的?
    • 我给了你一个通用的解决方案。但是您关心的只是通知声音对吗?
    • 是的。播放通知声音时应打开音量。之后,设置应恢复为初始值(例如,手机再次静音)。所以你的代码确实有很大帮助。但我需要一种在通知声音结束后调用“setStreamMute”的方法......
    • previousNotificationVolume 用于将其设置为之前的状态。因为有可能通知音量设置为 1 非常低不静音。所以必须在你的通知播放后设置为 1。这就是我不将其设置为静音模式的原因。
    猜你喜欢
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多