Probably a look in the sourcecode of this app will help. I found a class AsyncRingtonePlayer (https://android.googlesource.com/platform/packages/apps/DeskClock/+/master/src/com/android/deskclock/AsyncRingtonePlayer.java) which might do the work.
The trick is to use a different Audiostream, I guess. Lets have a look a the startAlarm-method:
private void startAlarm(MediaPlayer player) throws IOException {
// do not play alarms if stream volume is 0 (typically because ringer mode is silent).
if (mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
if (Utils.isLOrLater()) {
player.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build());
}
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.setLooping(true);
player.prepare();
mAudioManager.requestAudioFocus(null, AudioManager.STREAM_ALARM,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
player.start();
}
}
The audio stream type of the player object will be set to AudioManager.STREAM_ALARM. I guess that this could be the solution. I do not tested it, but this the first thing I found in the source code.
Please be also aware of some needed permissions as listed in the AndroidManifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<!-- WRITE_SETTINGS is required to record the upcoming alarm prior to L -->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- READ_PHONE_STATE is required to determine when a phone call exists prior to M -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- READ_EXTERNAL_STORAGE is required to play custom ringtones from the SD card prior to M -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
(https://android.googlesource.com/platform/packages/apps/DeskClock/+/master/AndroidManifest.xml)