首页 文章

Android Notification仅播放声音

提问于
浏览
8

我的活动中有倒数计时器,当它达到零时,我会向状态栏发送通知,该状态栏还会播放自定义wav文件以提醒用户 . 我只希望在用户当时没有使用应用程序的情况下显示状态栏通知,但我仍然希望无论计时器应用程序是否可见,都可以播放wav文件 .

是否可以让Android通知仅播放音频警报?我尝试使用MediaPlayer来播放wav文件,但这会使用自己的音量设置,而不是常规通知音量,所以如果你的媒体音量很低,那么通知声音也会以低音量播放,这是我不想要的 . 我希望声音以与其他通知相同的音量播放 .

我正在使用这个:http://developer.android.com/guide/topics/ui/notifiers/notifications.html

并提供这样的声音:

notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);

4 回答

  • 0

    http://developer.android.com/guide/topics/ui/notifiers/notifications.html的'Adding a sound'部分,有这样的说明:

    注意:如果默认字段包含DEFAULT_SOUND,则默认声音将覆盖声场定义的任何声音 .

    因此,如果您只想自定义声音,可以使用 -

    notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
    notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
    
  • 0

    根据我的实验,您确实可以发送通知,而不会出现在通知区域中,但仍然会播放声音 .

    您需要做的就是构建通知构建器并为其设置声音,但跳过待处理的意图,图标,内容 Headers 和内容文本 .

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSound(your_sound_uri);
    notificationManager.notify(1234, builder.build());
    
  • 5
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    
    Notification mNotification = new Notification.Builder(this)
    
                .setContentTitle("New Post!")
                .setContentText("Here's an awesome update for you!")
                .setSmallIcon(R.drawable.ic_lancher)
                .setContentIntent(pIntent)
                .setSound(soundUri)
                .build();
    
  • 4
    NotificationCompat.Builder builder= new NotificationCompat.Builder(this);
    

    three ways to setSound

    builder.setDefaults(Notification.DEFAULT_SOUND);
    builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
    

相关问题