Home  >  Android

Android Ringtone and RingtoneManager Example | Get Default and Current Ringtone

By Arvind Rai, June 03, 2015
This page will walk through the android Ringtone and RingtoneManager Example and provide the demo to get default and current ringtone. Ringtone and RingtoneManager classes are used for all types of sounds such as phone ringer, notification, alarm etc. Ringtone instance is obtained by RingtoneManager by passing type of sound. In our example I will discuss some public methods of Ringtone and RingtoneManager and will provide the demo to play current ringtone and display default and current ringtone title.

Ringtone

android.media.Ringtone API handles sound related task like ringtone, notification, alarm etc. We get the instance of Ringtone using RingtoneManager. Some public methods of Ringtone are described as below.

getAudioAttributes(): Returns AudioAttributes instance.
getTitle(Context context) : Gets the title of sound.
isPlaying() : Checks if sound is playing or not.
play() : On calling of this method, sound starts playing.
stop() : It stops the playing sound.

RingtoneManager

android.media.RingtoneManager is used to access all types of sounds like ringtone, notification and alarm etc. Using RingtoneManager, we get the instance of Ringtone. Find some constants by which we differentiate sound type.
TYPE_ALARM: refers sound type as alarm.
TYPE_NOTIFICATION: Indicates sounds to be notification.
TYPE_RINGTONE: Refers sounds to be phone ringer.

Now find some public methods of RingtoneManager.

getActualDefaultRingtoneUri(Context context, int type): Returns Uri of current default sounds.
getCursor(): Returns Cursor for the ringtones.
getRingtone(Context context, Uri ringtoneUri): On passing ringtone Uri and context, it returns Ringtone.

Get Default Ringtone using Settings.System.DEFAULT_RINGTONE_URI

Using Settings.System.DEFAULT_RINGTONE_URI, we can get default ringtone as follows.
Ringtone defaultRingtone = RingtoneManager.getRingtone(getActivity(),
                                   Settings.System.DEFAULT_RINGTONE_URI);
 

Get Current Ringtone

We can write the code as below to get current phone ringer.
Uri currentRintoneUri = RingtoneManager.getActualDefaultRingtoneUri(getActivity()
       		.getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
Ringtone currentRingtone = RingtoneManager.getRingtone(getActivity(), currentRintoneUri);  

Complete Example

Here I am providing a complete example. I will demo to fetch default and current ringtone title and will play current ringtone, too.

Fragment and FragmentActivity Classes

Find the Fragment and FragmentActivity Classes.
OutputFragment.java
package com.concretepage.android;
import android.app.Fragment;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.cp.android.R;
public class OutputFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup viewGroup, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.output_fragment, viewGroup, false);
        TextView output;
        output= (TextView)view.findViewById(R.id.ringtone);
        //fetch default Ringtone
    	Ringtone defaultRingtone = RingtoneManager.getRingtone(getActivity(), 
		                               Settings.System.DEFAULT_RINGTONE_URI);
        //fetch current Ringtone
        Uri currentRintoneUri = RingtoneManager.getActualDefaultRingtoneUri(getActivity()
        		.getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
        Ringtone currentRingtone = RingtoneManager.getRingtone(getActivity(), currentRintoneUri);
        //display Ringtone title
        output.setText(defaultRingtone.getTitle(getActivity())+" and " +
        		"Current Ringtone:"+currentRingtone.getTitle(getActivity()));
        //play current Ringtone
        currentRingtone.play();
        return view;
    }
} 
MainActivity.java
package com.concretepage.android;
import com.cp.android.R;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
} 

Layout XML Files

Find the layout XML files used in demo.
output_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:gravity="center"
    android:background="#b0b0ff"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="#FF0000"
        android:textSize="20sp"
        android:id="@+id/ringtone"/>
</LinearLayout> 
main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal" >
    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.concretepage.android.OutputFragment"
        android:id="@+id/output"/>
</LinearLayout> 

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">Ringtone Demo</string>
   <string name="hello">Hello World!</string>
</resources> 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cp.android"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="11" />
    <application
        android:allowBackup ="false"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.concretepage.android.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest> 

Output

Android Ringtone and RingtoneManager Example | Get Default and Current Ringtone

Download Source Code

Download APK file

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us