I'm trying to implement a simple alarm in android by using the MediaPlayer. However, every time I try to prepare() it, I get an error. Here's my code. I'm a total beginner concerning Java and Android so perhaps I'm doing something inherently wrong.
private void playDiscreteAlarm()
{
alarmSound = new MediaPlayer();
alarmSound.setAudioStreamType(AudioManager.STREAM_RING);
Resources res=context.getResources();
AssetFileDescriptor fd = res.openRawResourceFd(R.raw.discrete_alarm);
try {
alarmSound.setDataSource(fd.getFileDescriptor());
fd.close();
alarmSound.setLooping(true);
alarmSound.prepare();
alarmSound.start();
}
catch (IOException e)
{
Log.d("error\n");
}
}
The weird thing is that this worked once and after that stopped working.
It works when I use MediaPlayer.create()
however I need to use the ringer volume instead of the media volume, and I believe this is the way to do it.