【问题标题】:How to set a custom sound to a notification [duplicate]如何为通知设置自定义声音[重复]
【发布时间】:2017-11-02 03:00:14
【问题描述】:

我知道这个问题之前已经回答过了,但我仍然无法解决。我已经将 mp3 放入原始文件夹,并且我知道到它的路径是有效的,因为我使用 Ringtone 和 MediaPlayer 类进行了测试。通知创建得很好,但没有声音(而且我的设备上的音量最大)。这是我的代码

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)   
.setLargeIcon(icon)   
.setSmallIcon(R.drawable.logo)  
.setContentTitle(notification.getTitle())   
.setContentText(notification.getBody())   
.setSound(sound)   
.setPriority(Notification.PRIORITY_HIGH);     
mBuilder.setContentIntent(contentIntent);   
Notification mNotification = mBuilder.build();   
mNotification.sound = sound;   
NotificationManager mNotificationManager = (NotificationManager) context    
.getSystemService(Context.NOTIFICATION_SERVICE); 
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mNotification);

【问题讨论】:

标签: java android firebase


【解决方案1】:

试试这个

 notification.sound =Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.FILE_NAME);//Here is FILE_NAME is the name of file that you want to play

(或)

MediaPlayer mp= MediaPlayer.create(contexto, R.raw.your_sound);
                mp.start();

【讨论】:

  • 我已经这样做了。我测试过,它听起来像 MediaPlayer 类,但不是在通知中
  • notification.sound =Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.FILE_NAME); 试试这个..我希望这会工作......如果不工作让我知道
猜你喜欢
  • 1970-01-01
  • 2016-09-16
  • 1970-01-01
  • 2017-03-10
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
  • 2016-06-28
  • 1970-01-01
相关资源
最近更新 更多