Nitish
Nitish

Reputation: 3155

Android: Playing own sound on alarm

My app has feature of alarm. My requirement is to play own sound when alarm rings up but I am unable to do this. It is only showing notification when alarm rings up. The sound file which I want to play is inside raw folder in Res. Below I am posting my code:

In my activity class:

Intent AlarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
        AlarmIntent.putExtra("Ringtone", 
                Uri.parse("getResources().getResourceName(R.raw.shankh_final_mid)"));
        PendingIntent Sender = PendingIntent.getBroadcast(this, 0, AlarmIntent, 0);
        AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
        AlmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 
                (60 * 1000), (24 * 60 * 60 * 1000), Sender);

In receiver class:

public void onReceive(Context context, Intent intent) { 

    Intent in = new Intent(context, SnoozeEvent.class);
    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent Sender = PendingIntent.getActivity(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
    manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    notification = new Notification(R.drawable.icon, "Wake up alarm", System.currentTimeMillis());
    notification.setLatestEventInfo(context, "Hanuman Chalisa", "Wake Up...", Sender);
    notification.flags = Notification.FLAG_INSISTENT;
    notification.sound = (Uri)intent.getParcelableExtra("Ringtone");
    manager.notify(1, notification);  
}

Upvotes: 1

Views: 11129

Answers (4)

nburn42
nburn42

Reputation: 737

This answer was a bit outdated.

I had to open my own question here to play on the alarm channel.

Here is my working solution.

mediaPlayerScan = new MediaPlayer();
try {
  mediaPlayerScan.setDataSource(getContext(),
          Uri.parse(getString(R.string.res_path) + R.raw.scan_beep));

  if (Build.VERSION.SDK_INT >= 21) {
    mediaPlayerScan.setAudioAttributes(new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_ALARM)
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .build());
  } else {
    mediaPlayerScan.setAudioStreamType(AudioManager.STREAM_ALARM);
  }
  mediaPlayerScan.prepare();
} catch (IOException e) {
  e.printStackTrace();
}

Upvotes: 0

iTurki
iTurki

Reputation: 16398

Try to change those two lines:

//In your activity:
AlarmIntent.putExtra("Ringtone",getResources().getResourceName(R.raw.shankh_final_mid));
....   
//In your reciever
notification.sound = (Uri)(Uri.parse(intent.getStringExtra("Ringtone")));

Upvotes: 1

iAndroid
iAndroid

Reputation: 951

You can set custom sound in your bundle or into SD Card.only set path of your sound file as a notification sound.use below code.

notification.sound = Uri.parse("android.resource://my.package.name/raw/notification");

Upvotes: 5

Pattabi Raman
Pattabi Raman

Reputation: 5854

Try this code to make your own notification sound:

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, YourActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
Notification note  = new Notification(R.drawable.icon, "Your Text to Show", System.currentTimeMillis());
note.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
int nid = 123456789;
manager.notify(nid, note);

Upvotes: 0

Related Questions