Android alarm clock sound using the alarm volume setting?

1.6k Views Asked by At

I am trying to build a custom type of alarm clock for Android, and I have set up a settings page to allow the users to choose which alarm sound they would like to use. After they select it, it is placed into SharedPreferences, easy enough.

The problem I am having is how to play the alarm they have chosen, while using the RingtoneManager.TYPE_ALARM, so that the volume is based off the systems alarm sound instead of the systems notification volume.

I have tried multiple ways to even get it to use the Alarm volume, but nothing seems to be doing the trick.

1.

Uri currentRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
currentRingtone = RingtoneManager.getRingtone(context, currentRingtoneUri);
currentRingtone.play();

2.

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String syncConnPref = sharedPref.getString("alarm_new_alarm_sound", null);

if (syncConnPref != null) {
  RingtoneManager.getRingtone(this, Uri.parse(syncConnPref)).play();
}

I cannot seem to find any other direct ways of playing the actual alarm volume sound. Everything online looks at just playing ringtones.

I would really appreciate if anyone had any insight on using the alarm volume.

1

There are 1 best solutions below

1
On

Somewhat old thread, but maybe this will help someone in the future.
I am currently working on an alarm clock myself and I'm doing this:

int audioFile = prefs.getInt("audioFile", R.raw.yourFile);
MediaPlayer mp = new MediaPlayer();

try {
    mp.reset();

    mp.setDataSource(PlayerActivity.this,    
    Uri.parse("android.resource://your.package.here/" + audioFile));

    mp.setAudioStreamType(STREAM_ALARM);
    mp.setLooping(true);
    mp.prepare();
    mp.start();
}

catch (...) {
    ...
}

It's working fine for me.