Monday, June 22, 2015

Generate Alarm, Notification and Ringtone using RingtoneManager

RingtoneManager provides access to ringtones, notification, and other types of sounds. Here is a example to play and stop sound of Alarm, Notification and Ringtone using RingtoneManager.


com.example.androidringtone.MainActivity.java
package com.example.androidringtone;

import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    RadioButton optAlarm, optNotification, optRingtone;
    Button btnStart, btnStop;
    Ringtone ringTone;

    Uri uriAlarm, uriNotification, uriRingtone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        optAlarm = (RadioButton)findViewById(R.id.optAlarm);
        optNotification = (RadioButton)findViewById(R.id.optNotification);
        optRingtone = (RadioButton)findViewById(R.id.optRingtone);
        btnStart = (Button)findViewById(R.id.start);
        btnStop = (Button)findViewById(R.id.stop);

        uriAlarm = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        uriNotification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        uriRingtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        optAlarm.setText("uriAlarm: " + uriAlarm);
        optNotification.setText("uriNotification: " + uriNotification);
        optRingtone.setText("uriRingtone: " + uriRingtone);

        btnStart.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                if(ringTone!=null){
                    //previous ringTone is playing,
                    //stop it first
                    ringTone.stop();
                }

                if(optAlarm.isChecked()){
                    ringTone = RingtoneManager
                        .getRingtone(getApplicationContext(), uriAlarm);
                }else if(optNotification.isChecked()){
                    ringTone = RingtoneManager
                        .getRingtone(getApplicationContext(), uriNotification);
                }else if(optRingtone.isChecked()){
                    ringTone = RingtoneManager
                        .getRingtone(getApplicationContext(), uriRingtone);
                }

                Toast.makeText(MainActivity.this,
                    ringTone.getTitle(MainActivity.this),
                    Toast.LENGTH_LONG).show();
                ringTone.play();
            }
        });

        btnStop.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                if(ringTone!=null){
                    ringTone.stop();
                    ringTone = null;
                }
            }
        });
    }

}


layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/optAlarm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Alarm"
            android:checked="true" />

        <RadioButton
            android:id="@+id/optNotification"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Notification" />

        <RadioButton
            android:id="@+id/optRingtone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ringtone" />

    </RadioGroup>

    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start"/>

    <Button
        android:id="@+id/stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop"/>

</LinearLayout>



Next:
- Android example using ACTION_RINGTONE_PICKER

2 comments:

  1. Hey, i'm just wondering how to add sound to alarm alert by using RingtoneManager.TYPE_RINGTONE (like without pressing start like it will automatically ring when the alarm set)? and the sound will stop when the stop button is pressed?

    ReplyDelete