Android Alarm Start startAlarmRing(Context c, String tone)

Here you can find the source of startAlarmRing(Context c, String tone)

Description

Start ringing the alarm using TONE and return the MediaPlayer instance

Declaration

public static MediaPlayer startAlarmRing(Context c, String tone) 

Method Source Code

//package com.java2s;
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  .j  a  v a2  s. com*/
     * Start ringing the alarm using TONE and return the MediaPlayer instance
     */
    public static MediaPlayer startAlarmRing(Context c, String tone) {
        MediaPlayer mPlayer = new MediaPlayer();
        Uri toneUri;
        try {
            if (tone != null && !tone.equals("")) {
                toneUri = Uri.parse(tone);
            } else {
                toneUri = RingtoneManager
                        .getDefaultUri(RingtoneManager.TYPE_ALARM);
            }
        } catch (Exception e) {
            toneUri = RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_ALARM);
        }
        try {
            if (toneUri != null) {
                mPlayer.setDataSource(c, toneUri);
                mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                mPlayer.setLooping(true);
                mPlayer.prepare();
                mPlayer.start();
            }
        } catch (Exception e) {
            // do nothing
        }
        return mPlayer;
    }
}

Related

  1. startAlarm(Context context, int type, int index, int hour, int minute, String action)
  2. startAlarm(Context context, int type, int index, int interval, String action)
  3. start(Context context, Intent broadcastIntent, int requestCode, long delayAtMillis, long period)
  4. startImmediately(Context context, Intent broadcastIntent, int requestCode, long period)