Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
644 views
in Technique[技术] by (71.8m points)

audio - Android Notification to play sound only

I have a countdown timer in my activity and when it reaches zero I send a notification to the status bar which also plays a custom wav file to alert the user. I only want the status bar notification to appear if the user isn't using the app at the time but I still want the wav file to play whether the timer app is visible or not.

Is it possible to make the Android notification only play the audio alert? I've tried using MediaPlayer to play the wav file instead but this uses its own volume settings and not the regular notification volume so if your media volume is low then the notification sound plays at a low volume too which I don't want. I want the sound to play at the same volume as other notifications.

I'm using this: http://developer.android.com/guide/topics/ui/notifiers/notifications.html

And supplying the sound like this:

notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In 'Adding a sound' section in http://developer.android.com/guide/topics/ui/notifiers/notifications.html there is this note :

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

So, if you want to customize only sound, you may use -

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...