I am implementing Notification as below. Default Alarm sound is playing fine, but only for once. All i want is to play it repeatedly until the tap is registered.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("I Am Fine")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(NOTIFICATION_MESSAGE))
.setContentText(NOTIFICATION_MESSAGE)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
mBuilder.setSound(alarmSound,AudioManager.STREAM_MUSIC);
The second parameter to setSound doesn’t showing any effect. Please help !
You have to use FLAG_INSISTENT for the notification. From the documentation:-
public static final int FLAG_INSISTENT
Bit to be bitwise-ored into the flags field that if set, the audio will be
repeated until the notification is cancelled or the notification window is
opened.Constant Value: 4 (0x00000004)
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Uri soundUri = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
getApplicationContext()).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("title").setContentText("message")
.setSound(soundUri); // This sets the sound to play
Notification mNotification = mBuilder.build();
mNotification.flags |= Notification.FLAG_INSISTENT;
// Display notification
notificationManager.notify(1, mNotification);
Answer:
I have called this under SendNotification Method Body.
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PlaySound(this,notification);
} catch (Exception e) {
e.printStackTrace();
}
PlaySound Definition is as below:
private void PlaySound(Context context,Uri alert){
try{
mPlayer=new MediaPlayer();
mPlayer.setDataSource(context,alert);
final AudioManager am=(AudioManager) getSystemService(Context.AUDIO_SERVICE);
if(am.getStreamVolume(AudioManager.STREAM_ALARM)!=0);
{
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mPlayer.prepare();
mPlayer.setLooping(true);
mPlayer.start();
}
}catch(IOException e)
{
Log.i("AlaramReciever", "no audio file");
}
}
To stop the sound when notification is tapped, i’ve changed the MediaPlayer object to a public static object as below:
if(GCMNotificationIntentService.mPlayer != null)
{
try{
GCMNotificationIntentService.mPlayer.stop();
GCMNotificationIntentService.mPlayer.release();
}finally {
GCMNotificationIntentService.mPlayer = null;
}
}
The desired functionality is achieved, but i keep getting following RunTimeException
Handler android.media.MediaPlayer$EventHandler that is sending message to a Handler on a dead thread as the onDestroy immediately being called. Is there any way to prevent this ?
Answer:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
MediaPlayer player = MediaPlayer.create(this, notification);
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.setLooping(true);
player.start();
player.setLooping(true);
this will allow you media player to to play sound repeatedly. on onclick()
of button use player.stop();
to stop the sound
Answer:
You should mute the notification sounds and simply play the sound file from your app using media player. And set the instance of the media player to loop.
There after, you should add a pending intent variable with your notification, once the user taps on the notification, the associated event is fired and you can stop the media Player instance from here.
Tags: androidandroid, oop