【问题标题】:How to play an android notification sound如何播放安卓通知声音
【发布时间】:2011-05-25 09:20:11
【问题描述】:

我想知道如何在不通过媒体流播放的情况下播放通知声音。现在我可以通过媒体播放器做到这一点,但是我不希望它作为媒体文件播放,我希望它作为通知、警报或铃声播放。下面是我的代码现在的示例:

MediaPlayer mp = new MediaPlayer();
mp.reset();
mp.setDataSource(notificationsPath+ (String) apptSounds.getSelectedItem());
mp.prepare();
mp.start();

【问题讨论】:

    标签: android audio notifications


    【解决方案1】:

    我也有几乎相同的问题。经过一番研究,我认为如果你想播放默认的系统“通知声音”,你几乎必须显示一个通知并告诉它使用默认声音。对于其他一些答案中的论点,如果您正在播放通知声音,您也应该呈现一些通知消息。

    不过,只要稍微调整一下通知 API,您就可以接近您想要的。您可以显示一个空白通知,然后在几秒钟后自动将其删除。我认为这对我有用;也许它会为你工作。

    我在com.globalmentor.android.app.Notifications.java 中创建了一组便捷方法,可让您创建如下通知声音:

    Notifications.notify(this);
    

    LED 也会闪烁,如果您有振动许可,则会发生振动。是的,通知栏中会出现一个通知图标,但会在几秒钟后消失。

    此时您可能会意识到,既然通知无论如何都会消失,那么您不妨在通知栏中有一个滚动的股票信息;你可以这样做:

    Notifications.notify(this, 5000, "This text will go away after five seconds.");
    

    这个类中还有很多其他的便捷方法。您可以从其 Subversion 存储库下载整个库并使用 Maven 构建它。它依赖于 globalmentor-core 库,也可以使用 Maven 构建和安装。

    【讨论】:

    【解决方案2】:

    给通知频道设置声音

            Uri alarmUri = Uri.fromFile(new File(<path>));
    
            AudioAttributes attributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build();
    
            channel.setSound(alarmUri, attributes);
    

    【讨论】:

      【解决方案3】:
      Intent intent = new Intent(this, MembersLocation.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          intent.putExtra("type",type);
          intent.putExtra("sender",sender);
          PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                  PendingIntent.FLAG_ONE_SHOT);
      
          String channelId = getString(R.string.default_notification_channel_id);
      
          Uri Emergency_sound_uri=Uri.parse("android.resource://"+getPackageName()+"/raw/emergency_sound");
         // Uri Default_Sound_uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          if(type.equals("emergency"))
          {
              playSound=Emergency_sound_uri;
          }
          else
          {
              playSound= Settings.System.DEFAULT_NOTIFICATION_URI;
          }
      
          NotificationCompat.Builder notificationBuilder =
                  new NotificationCompat.Builder(this, channelId)
                          .setSmallIcon(R.drawable.ic_notification)
                          .setContentTitle(title)
                          .setContentText(body)
                          .setSound(playSound, AudioManager.STREAM_NOTIFICATION)
                          .setAutoCancel(true)
                          .setColor(getColor(R.color.dark_red))
                          .setPriority(NotificationCompat.PRIORITY_HIGH)
                          .setContentIntent(pendingIntent);
      
         // notificationBuilder.setOngoing(true);//for Android notification swipe delete disabling...
      
          NotificationManager notificationManager =
                  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      
          // Since android Oreo notification channel is needed.
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              NotificationChannel channel = new NotificationChannel(channelId,
                      "Channel human readable title",
                      NotificationManager.IMPORTANCE_HIGH);
              AudioAttributes att = new AudioAttributes.Builder()
                      .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                      .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                      .build();
              channel.setSound(Emergency_sound_uri, att);
              if (notificationManager != null) {
                  notificationManager.createNotificationChannel(channel);
              }
          }
      
          if (notificationManager != null) {
              notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
          }
      }
      

      【讨论】:

      • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation 将通过展示为什么这是解决问题的好方法,并使其对有其他类似问题的未来读者更有用,从而大大提高其长期价值。请edit您的回答添加一些解释,包括您所做的假设。
      【解决方案4】:

      如果要播放默认通知声音,可以使用NotificationCompat.Builder类的setDefaults(int)方法:

      NotificationCompat.Builder mBuilder =
              new NotificationCompat.Builder(this)
                      .setSmallIcon(R.drawable.ic_notification)
                      .setContentTitle(getString(R.string.app_name))
                      .setContentText(someText)
                      .setDefaults(Notification.DEFAULT_SOUND)
                      .setAutoCancel(true);
      

      我相信这是完成任务的最简单方法。

      【讨论】:

      • 应用在后台时不播放声音。有什么建议吗?
      【解决方案5】:

      试试这个:

      public void ringtone(){
          try {
              Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
              Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
              r.play();
           } catch (Exception e) {
               e.printStackTrace();
           }
      }
      

      【讨论】:

        【解决方案6】:

        如果有人仍在寻找解决方案,我在 How to play ringtone/alarm sound in Android 找到了答案

        try {
            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
            r.play();
        } catch (Exception e) {
            e.printStackTrace();
        }
        

        您可以将 TYPE_NOTIFICATION 更改为 TYPE_ALARM,但您需要跟踪您的铃声 r 以停止播放...例如,当用户单击按钮或其他内容时。

        【讨论】:

        • 例如,摩托罗拉手机扩展了偏好活动,并允许用户为短信和其他类别定义通知声音。上述方法不适用于此类手机。你知道如何解决这个问题吗?
        • 我收到了一个错误:MediaPlayer - Should have subtitle controller already set。这是什么意思?
        • 使用此解决方案,在 28/29 次后,声音停止播放。有谁知道为什么?
        • 为什么要捕获每个异常?哪个可以扔?
        • @Sam,是的。 stackoverflow.com/q/39876885/2614353.Basicly,不要在每次播放时都创建 RingTone 对象。创建一次,然后多次播放同一个对象。
        【解决方案7】:

        您现在可以通过在构建通知时包含声音而不是单独调用声音来做到这一点。

        //Define Notification Manager
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        
        //Define sound URI
        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(icon)
                .setContentTitle(title)
                .setContentText(message)
                .setSound(soundUri); //This sets the sound to play
        
        //Display notification
        notificationManager.notify(0, mBuilder.build());
        

        【讨论】:

        • 这解决了一个不同的问题——不是“如何播放通知声音”,而是“如何播放通知显示声音”。接受的答案在其解决方案中是合理的。
        • 也许您还应该将其设置为通过 STREAM_NOTIFICATION 播放,以便使用操作系统当前的通知音量偏好播放:.setSound(soundUri, AudioManager.STREAM_NOTIFICATION)
        • @Rob Riddle 它工作正常。但在多个通知的情况下,例如100 个通知与下一个通知声音混合在一起。从逻辑上讲,如果声音已经在播放,它应该等待之前的播放完成。在这种情况下你能帮忙吗?
        • 应用在后台时不播放声音。有什么建议吗?
        【解决方案8】:

        您的问题已经有一段时间了,但是...您是否尝试过设置音频流类型?

        mp.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        

        必须在准备之前完成。

        【讨论】:

          【解决方案9】:

          我认为“通知声音”的概念对于 Android UI 来说是错误的。

          Android 的预期行为是使用标准通知来提醒用户。如果您播放没有状态栏图标的通知声音,您会让用户感到困惑(“那是什么声音?这里没有图标,也许我有听力问题?”)。

          例如,如何在通知上设置声音:Setting sound for notification

          【讨论】:

          • 不是真的,这可能是应用内通知。例如,如果您在聊天应用程序中,并且对传入和传出的消息具有较小的声音效果。这些本质上是通知,您希望它们在手机处于静音模式时关闭。
          • 嗯,你是对的,但那是另一回事。我正在谈论(假设这是问题的主题)“系统通知声音”,即在系统 Android GUI 上。当然,当您进入自己的应用程序时,一切由您自己决定。
          【解决方案10】:

          您可以使用NotificationNotificationManager 来显示您想要的通知。然后,您可以自定义要播放通知的声音。

          【讨论】:

          • 我不想显示通知,我只想播放声音。
          猜你喜欢
          • 2018-11-18
          • 1970-01-01
          • 1970-01-01
          • 2014-06-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-14
          相关资源
          最近更新 更多