2011-12-07 217 views
7

我的活動中有一個倒數計時器,當它達到零時,我會向狀態欄發送通知,該通知還會播放自定義wav文件以提醒用戶。我只想要狀態欄通知出現,如果用戶當時沒有使用應用程序,但我仍然希望播放wav文件,無論定時器應用程序是否可見。只有播放聲音的Android通知

是否可以讓Android通知只播放音頻提示?我試過使用MediaPlayer來播放wav文件,但是它使用自己的音量設置,而不是常規的通知音量,所以如果您的媒體音量很低,通知聲音也會以低音量播放,這也是我不想要的。我希望聲音以與其他通知相同的音量播放。

我使用這個: http://developer.android.com/guide/topics/ui/notifiers/notifications.html

和供應的聲音是這樣的:

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

是的,它可以只播放一段音頻警報。 –

+1

謝謝@ coder_For_Life22,你會告訴我如何? – Daniel

回答

5

在 '添加聲音' 在http://developer.android.com/guide/topics/ui/notifiers/notifications.html 節有這樣一個字條:

Note: If the defaults field includes DEFAULT_SOUND, then the default sound overrides any sound defined by the sound field.

所以,如果你只想定製聲音,你可以使用 -

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

根據我的實驗,確實可以發送通知,但不會將其顯示在通知區域中,但仍會播放聲音。

您只需構建通知構建器併爲其設置聲音,但跳過待處理的意圖,圖標,內容標題和內容文本即可。

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

這些答案特別有用,因爲文檔對此有錯誤。 http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CreateNotification說,「通知對象*必須包含以下內容:一個小圖標,由setSmallIcon()設置;一個標題,一組通過setContentTitle();詳細文本,由setContentText()設置「 – Jerry101

+2

該圖標是必需的。這會導致錯誤:java.lang.IllegalArgumentException:無效的通知(無效的小圖標) –

0
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(); 
0
NotificationCompat.Builder builder= new NotificationCompat.Builder(this); 

三種方式setSound

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