The Media provider contains meta data for all available media on both internal and external storage devices.
What are we going to do?
We will make a recording application and set the recorded sounds as alarm tone, pls, don't hesitate to comment if you are having problems.
The activitymain.xml
The MainActivity (You can copy it all and paste to your activity)
import java.io.File; import java.io.IOException; import android.content.ContentValues; import android.content.Intent; import android.media.MediaRecorder; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends ActionBarActivity { Button startButton; Button stopButton; MediaRecorder recorder; String path="/sdcard/media/alarms/"; String audioname = "sample.mp4"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startButton = (Button) findViewById(R.id.button1); stopButton = (Button) findViewById(R.id.button2); stopButton.setEnabled(false); //make directory boolean exists = (new File(path)).exists(); if (!exists){new File(path).mkdirs();} // } public void onStartClicked(View v) throws Exception{ startButton.setEnabled(false); stopButton.setEnabled(true); recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC ); // recorder.setMaxDuration(50000); // 50 seconds recorder.setAudioEncodingBitRate(160 * 1024); //recorder.setAudioChannels(2); recorder.setOutputFile(path+audioname ); //try and catch try { recorder.prepare(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } recorder.start(); } public void onStopClicked(View v) { startButton.setEnabled(true); stopButton.setEnabled(false); recorder.stop(); recorder.reset(); recorder.release(); recorder = null; addRecordingToMediaLibrary(); Toast.makeText(getApplicationContext(), "Audio recorded successfully", Toast.LENGTH_LONG).show(); } protected void addRecordingToMediaLibrary() { ContentValues values = new ContentValues(4); long current = System.currentTimeMillis(); values.put(MediaStore.MediaColumns.DATA, path+audioname ); values.put(MediaStore.MediaColumns.TITLE, path+audioname ); values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000)); values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp"); //new 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, true); values.put(MediaStore.Audio.Media.IS_MUSIC, false); ///new // values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath()); // ContentResolver contentResolver = getContentResolver(); //Insert it into the database this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(path+audioname), values); //RingtoneManager.setActualDefaultRingtoneUri(Activity.this, // RingtoneManager.TYPE_RINGTONE, newUri); sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+path+audioname + Environment.getExternalStorageDirectory()))); // sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+editText1))); //Toast.makeText(this, "New Alarm Sound " + editText1 +format, Toast.LENGTH_LONG).show(); } }And add this permission to your Android Manifest:
Here i named the audio as sample, and also, i made a directory for you "/sdcard/media/alarms/",(you really need this code someday) ... Now go to your Alarm application and find the "sample.mp4" into the alarm ringtone... now don't hesitate to comment if your having problems!