Android set ringtone

If your application has a functionality to set ringtone of an Android device, this Android tip is useful to you. I am going to show you how to read a list of ringtone files from the external storage to display in a ListView so that the user is able to select a ringtone to be used as the default ringtone of the device and play it.
To set a default ringtone to the Android device, you use the setActualDefaultRingtoneUri() method of the RingtoneManager class. However, this does not work unless you insert the ringtone to the Audio.Media content provider using the insert() method of ContentResolver.
To play the ringtone from your application, you can use the MediaPlayer class. The data source of the MediaPlayer object is the Uri object of the default ringtone. Don't forget to specify AudioManager.STREAM_RING as the audio stream type before you start playing the ringtone.
Now to have example application on setting ringtone, you create an Android project. Then modify the activity_main.xml file to add a ListView as shown below. The ListView displays ringtone files in the ring_tones folder of the external storage. From the list, the user can select a ringtone to be used as the default ringtone.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   tools:context="com.example.androidexamples.MainActivity" >

   <ListView
     android:id="@+id/listtones"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
    
     />

</RelativeLayout>


Then you create a row layout file of the ListView in the res/layout folder. It is called row.xml that has one TextView to show the ringtone files.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="horizontal" >
  
   <TextView
   android:id="@+id/tone"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="10sp"
   android:textStyle="bold"
   android:textSize="18sp"
   >
</TextView>


</LinearLayout>


In the MainActivity class that hosts the ListView, you write code to show the ringtone files in the ListView, insert the selected ringtone to the Audio.Media database, set the selected ringtone as the default ringtone, and play the default ringtone.

package com.example.androidexamples;

import java.io.File;
import java.io.IOException;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore.Audio;
import android.provider.MediaStore.MediaColumns;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View;

public class MainActivity extends ActionBarActivity {

ListView listView;
ArrayAdapter<String> adapter;
Activity context;
MediaPlayer mMediaPlayer;
final String tone_dir=Environment.getExternalStorageDirectory()+"/ring_tones";
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     context=this;
     loadTones();
    
   }

   public void playDefaultTone(){
   // RingtoneManager ringtoneManager = new RingtoneManager(this);
     Uri ringToneUri= RingtoneManager.getActualDefaultRingtoneUri(this,RingtoneManager.TYPE_RINGTONE);
     if(mMediaPlayer!=null){
     // Stop the previous play
     stopPlaying();
     }
    
     try {
    
       // Play a new ring tone
       mMediaPlayer = new MediaPlayer();
        
     mMediaPlayer.setDataSource(this,ringToneUri);

     mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);

     mMediaPlayer.prepare();

     mMediaPlayer.start();
     } catch (IllegalArgumentException e) {
     e.printStackTrace();
     } catch (SecurityException e) {
     e.printStackTrace();
     } catch (IllegalStateException e) {
     e.printStackTrace();
     } catch (IOException e) {
     e.printStackTrace();
     }
   }
   public void stopPlaying(){
  
   mMediaPlayer.stop();
   mMediaPlayer.release();
   mMediaPlayer=null;
  
   }
   public void setTone(String pathString){
   // Create File object for the specified ring tone path
   File f=new File(pathString);
   // Insert the ring tone to the content provider
   ContentValues value=new ContentValues();
   value.put(MediaColumns.DATA, f.getAbsolutePath());
   value.put(MediaColumns.TITLE, f.getName());
   value.put(MediaColumns.SIZE, f.length());
   value.put(MediaColumns.MIME_TYPE,"audio/*");
   value.put(Audio.Media.ARTIST, "artist");
   value.put(Audio.Media.DURATION, 500);
   value.put(Audio.Media.IS_ALARM, false);
   value.put(Audio.Media.IS_MUSIC, false);
   value.put(Audio.Media.IS_NOTIFICATION, false);
   value.put(Audio.Media.IS_RINGTONE, true);
   ContentResolver cr=getContentResolver();
   Uri url=Audio.Media.getContentUriForPath(f.getAbsolutePath());
   Uri addedUri=cr.insert(url, value);
   // Set default ring tone
   RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE,addedUri);
   }
   public void loadTones(){
   ListView listView=(ListView)findViewById(R.id.listtones);
   listView.setOnItemClickListener(new OnItemClickListener(){
   public void onItemClick(AdapterView<?> adapter, View view, int position, long id){
   ViewGroup vg=(ViewGroup)view;
   TextView tv=(TextView)vg.findViewById(R.id.tone);
   setTone(tone_dir+"/"+tv.getText().toString());
   playDefaultTone();
   }
   });
   File f=new File(tone_dir);
   if(f.exists()){
   String[] tone_names=f.list();
   adapter=new ArrayAdapter<String>(this, R.layout.row,R.id.tone,tone_names);
   listView.setAdapter(adapter);
    
   }
  
   }
  
}

This example project requires you to add the following permissions to the AndroidManifest.xml file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

Android-set-ringtone

Posted by: Dara | post date: 11-07-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.