【问题标题】:Android Notification to play sound onlyAndroid 通知仅播放声音
【发布时间】:2012-01-14 21:58:52
【问题描述】:

我的活动中有一个倒数计时器,当它达到零时,我会向状态栏发送通知,该状态栏还会播放自定义 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);

【问题讨论】:

  • 是的,可以只播放音频警报。
  • 谢谢@coder_For_Life22,你能告诉我怎么做吗?

标签: android audio notifications


【解决方案1】:

http://developer.android.com/guide/topics/ui/notifiers/notifications.html 的“添加声音”部分 有这个注释:

注意:如果默认字段包含 DEFAULT_SOUND,则默认声音会覆盖该声音字段定义的任何声音。

所以,如果你只想自定义声音,你可以使用 -

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

【讨论】:

    【解决方案2】:

    根据我的实验,你确实可以在不出现在通知区域的情况下发送通知,但仍然可以播放声音。

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

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSound(your_sound_uri);
    notificationManager.notify(1234, builder.build());
    

    【讨论】:

    • 这些答案特别有用,因为文档对此有误。 developer.android.com/guide/topics/ui/notifiers/… 说,“通知对象必须包含以下内容:小图标,由 setSmallIcon() 设置;标题,由 setContentTitle() 设置;详细文本,由 setContentText() 设置”
    • 该图标是必需的。这会导致错误:java.lang.IllegalArgumentException: Invalid notification (no valid small icon)
    【解决方案3】:
    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);
      

      设置声音的三种方式

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

      【讨论】:

        猜你喜欢
        • 2012-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-04
        • 2010-10-01
        相关资源
        最近更新 更多