// Do the common stuff when starting the alarm.
private void startAlarm(MediaPlayer player)
throws java.io.IOException, IllegalArgumentException,
IllegalStateException {
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
// do not play alarms if stream volume is 0
// (typically because ringer mode is silent).
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.setLooping(true);
player.prepare();
player.start();
}
}
private void setDataSourceFromResource(Resources resources,
MediaPlayer player, int res) throws java.io.IOException {
AssetFileDescriptor afd = resources.openRawResourceFd(res);
if (afd != null) {
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
afd.getLength());
afd.close();
}
}
private static final float IN_CALL_VOLUME = 0.125f;
// Check if we are in a call. If we are, use the in-call alarm
// resource at a low volume to not disrupt the call.
if (mTelephonyManager.getCallState()
!= TelephonyManager.CALL_STATE_IDLE) {
Log.v("Using the in-call alarm");
mMediaPlayer.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME);
setDataSourceFromResource(getResources(), mMediaPlayer,
R.raw.in_call_alarm);
} else {
mMediaPlayer.setDataSource(this, alert);
}
startAlarm(mMediaPlayer);