2016-07-23 142 views
0

我试图重写通知声音并播放我自己的通知声音。 当前的代码是:重写通知声音Android

NotificationCompat.Builder mBuilder = 
    new NotificationCompat.Builder(context) 
    .setSmallIcon(R.drawable.ic_notifications_white_24dp) 
    .setContentTitle(text) 
    .setColor(context.getResources().getColor(R.color.colorAccent)) 
    .setSound(soundUri); 
// Sets an ID for the notification 
int mNotificationId = 2580; 

// Gets an instance of the NotificationManager service 
NotificationManager mNotifyMgr = 
    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
// Builds the notification and issues it. 
mNotifyMgr.notify(mNotificationId, mBuilder.build()); 

但原来的通知重写(通过声音)我的声音。 有没有这样做的方法?

编辑:

当我试图同时播放声音正在播放的声音。有时候原始声音是第一声,而首先播放的是自定义声音。

我只想听到自定义声音。

+0

你是怎么定义soundUri的?如果正在播放默认声音,则可能表示setSound(soundUri)没有正确设置声音(例如错误的路径) – W0rmH0le

回答

0

创建res/raw文件夹,然后把你的通知声音到这个文件夹

试试这个

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

有提供自定义通知声音时,你应该记住的几件事情。

  • 声音乌里
    soundUri必须是这样的:

    Uri soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.yourSound);

  • 通知默认
    确保你没有使用.setDefaults(DEFAULT_SOUND | DEFAULT_ALL);
    许多开发人员已经观察到,使用.setDefaults()没有DEFAULT_SOUND以某种方式覆盖您的自定义声音。因此完全删除.setDefaults()

  • 声音文件长度
    我听说没有声音播放的报道,因为声音文件太长。尝试将其限制为5秒。

根据您发布的代码,唯一可能的错误可能是由于无效soundUri,所以给那看看。