I have done a lot of googling but other's solutions are not working for me.
My goal is to play a sound on demand on the alarm channel.
(So the sound volume is adjusted by the alarm volume setting)
from this thread I build the following code
mediaPlayerScan = MediaPlayer.create(getContext(), 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);
}
It still plays on the music channel. (IE volume is adjusted in music setting not alarm)
My intuition is that i'm missing a permission or something, but I haven't found such a permission.
I'm testing on a Google Pixel 1
Thanks,
Nathan
Edit:
Thanks to @jeffery-blattman the following code works for me
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();
}