The following function will show you how to set a sound as ringtone on android.
You need the following permissions set on Manifest file:
<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
The xml below contains a button. On pressing this button will call the function to save
the sound on sdcard and set it as ringtone. Code for ringtone.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/bg"> <ImageButton android:id="@+id/ImageButton01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/transparent" android:layout_marginLeft="180dip" android:layout_marginTop="200dip"></ImageButton> <TableLayout android:id="@+id/TableLayout01" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:layout_width="fill_parent"><TableRow android:id="@+id/TableRow01" android:layout_height="wrap_content" android:layout_width="fill_parent"> <Button android:id="@+id/Button01" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_width="fill_parent" android:layout_marginLeft="65dip" android:text="Set as Ringtone!"></Button></TableRow> </TableLayout></LinearLayout>
The following code is the function and the Ringtone class
package com.airhorn; import java.io.BufferedInputStream; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.app.ProgressDialog; import android.content.ContentValues; import android.content.Intent; import android.media.MediaPlayer; import android.media.RingtoneManager; import android.media.MediaPlayer.OnCompletionListener; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageButton; import android.widget.Toast; public class Ringtone extends Activity { MediaPlayer mp=new MediaPlayer(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ringtone); Button a=(Button)findViewById(R.id.Button01); a.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { if(saveas(R.raw.sound)){ Toast.makeText(Ringtone.this, "The sound was set as ringtone!", Toast.LENGTH_LONG).show(); }; }}); ImageButton w=(ImageButton)findViewById(R.id.ImageButton01); w.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { playSound(R.raw.sound); }}); } public void playSound(int fileId) { MediaPlayer mp; try { mp = MediaPlayer.create(this, fileId); mp.start(); mp.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mp.release(); mp = null; } }); } catch (Exception exception) { } } public boolean saveas(int ressound){ byte[] buffer=null; InputStream fIn = getBaseContext().getResources().openRawResource(ressound); int size=0; try { size = fIn.available(); buffer = new byte[size]; fIn.read(buffer); fIn.close(); } catch (IOException e) { // TODO Auto-generated catch block return false; } String path="/sdcard/sounds/"; String filename="mysound"+".mp3"; boolean exists = (new File(path)).exists(); if (!exists){new File(path).mkdirs();} FileOutputStream save; try { save = new FileOutputStream(path+filename); save.write(buffer); save.flush(); save.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block return false; } catch (IOException e) { // TODO Auto-generated catch block return false; } sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename))); File k = new File(path, filename); ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath()); values.put(MediaStore.MediaColumns.TITLE, "exampletitle"); values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); values.put(MediaStore.Audio.Media.ARTIST, "cssounds "); values.put(MediaStore.Audio.Media.IS_RINGTONE, true); values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false); values.put(MediaStore.Audio.Media.IS_ALARM, false); values.put(MediaStore.Audio.Media.IS_MUSIC, false); //Insert it into the database Uri newUri= this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values); RingtoneManager.setActualDefaultRingtoneUri( this, RingtoneManager.TYPE_RINGTONE, newUri ); return true; } }
I hope this can help you, so that you can set as ringtone any sound that you want in Android. If you see in this code, there are two buttons, one for playing the sound, and one for setting the sound as ringtone, so use both of functions for whatever you need.