Android examples for android.media:RingtoneManager
Play an alarm sound on the device
import java.io.IOException; import android.content.Context; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.RingtoneManager; import android.net.Uri; public class Main { /**//from w w w . jav a 2 s. c o m * Play an alarm sound on the device * * @param context * The context * @return MediaPlayer */ public static MediaPlayer playAlarmSound(Context context) { MediaPlayer mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(context, getAlarmUri()); final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) { mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); mediaPlayer.prepare(); mediaPlayer.start(); } return mediaPlayer; } catch (IOException e) { return null; } } /** * Get an alarm sound. Try for an alarm. If none set, try notification, * otherwise, ringtone. * * @return Alarm media uri */ private static Uri getAlarmUri() { Uri alarm = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); if (alarm == null) { alarm = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); if (alarm == null) { alarm = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); } } return alarm; } }