2016-07-23 164 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,所以給那看看。