Start ringing the alarm using TONE and return the MediaPlayer instance - Android Android OS

Android examples for Android OS:Alarm Start

Description

Start ringing the alarm using TONE and return the MediaPlayer instance

Demo 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  va2 s. c  o  m
     * 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 Tutorials