First Introduce Android the system supports the ringtone format. There are several types of the following :
64 Hz MIDI,AAC, aac+, AMR, WAV, MP3, Real Audio, WMA, OGG and other formats.
Setting the audio file to a ringtone is simple, just a few steps away:
1) get the URI of the system audio file
Uri Uri =mediastore.audio.media. Getcontenturiforpath (File.getabsolutepath ()); // gets the URI of the system audio file
2) Insert the file into the System Media Library and get the new URI
Uri Newuri = this. Getcontentresolver (). Insert (uri,values); // Insert the file into the System Media Library and get the new URI
3) set the ringtone
Ringtonemanager. Setactualdefaultringtoneuri (This, Ringtype, Newuri); // Set Ringtones
Here is the detailed code for setting the ringtone:
/** * Set ringtone * @param ringtype int: Ringtone type * @param file file: Files to be set as ringtones * */protected void setringtone (int ringtype, file file ) {//TODO auto-generated method Stubboolean Isringtone=false,isnotification=false,isalarm=false,ismusic=false; String msg= ""; switch (ringtype) {case constutil.ring_alarm://alarm isalarm=true;msg= "Set alarm ringtone! "; break;case constutil.ring_notification://notification isnotification=true;msg=" set notification ringtone success! "; break;case constutil.ring_ringtone://call isringtone=true;msg=" set the incoming ringtone success! "; case constutil.ring_music://add to Ringtone library (all ringtones) ismusic=true;msg=" Set all ringtones successfully! "; break;default:break;} Contentvalues values = new Contentvalues (); Values.put (MediaStore.MediaColumns.DATA, File.getabsolutepath ()); Values.put (MediaStore.MediaColumns.TITLE, File.getname ()); Values.put (MediaStore.MediaColumns.MIME_TYPE, "audio/* "); Values.put (MediaStore.Audio.Media.IS_RINGTONE, Isringtone); Values.put (MediaStore.Audio.Media.IS_NOTIFICATION , isnotification); Values.put (MediaStore.Audio.Media.IS_ALARM, isalarm); Values.put (mediastore.audio.media.iS_music, Ismusic); Uri uri = MediaStore.Audio.Media.getContentUriForPath (File.getabsolutepath ());//Get Uriuri of the system audio file Newuri = This.getcontentresolver (). Insert (URI, values);//Inserts the file into the System Media Library and gets the new Uriringtonemanager.setactualdefaultringtoneuri ( This,ringtype, Newuri);//Set Ringtone Toast.maketext (Getapplicationcontext (), Msg,toast.length_short). Show ();