2011-12-07 10 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

をsetSoundする方法はい、音声のみ、アラートを再生することが可能です。 –

+1

ありがとう@ coder_For_Life22、あなたは私にどのように表示されますか? – Daniel

答えて

5

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

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

をそして、このような音を供給

音だけをカスタマイズしたい場合は、-

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); 

builder.setDefaults(Notification.DEFAULT_SOUND); 
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI); 
関連する問題