views:

1157

answers:

7

I have sounds files in my res/raw folder and i want to select a sound to set as a ringtone on the click of a button. Wonder how can i do that?

+2  A: 

I use "Rings Extended" http://www.androidapps.com/t/rings-extended

With that app installed when you go to change your ringtone you will have the option to select Rings Extended. Also use "Ringdroid" to edit ringtones.

SuperJames
I want an example source code for incorporating this feature into my app.
Maxood
http://code.google.com/p/apps-for-android/source/browse/#svn/trunk/RingsExtended/src/com/example/android/rings_extended
SuperJames
A: 

Unfortunately, even though those are useful apps, it is not the answer Maxood was looking for. He is developing an android application himself which has integrated sounds (which are stored in the /res/raw folder in the dev tree and wants to know if there's a piece of code out there that will allow you to set those integrated sounds as ringtones.

I'm looking for this answer as well as i am also trying to accomplish this task. I know this is possible since there are already quite a few apps out there that can do this.

What would also be useful would be an option to save those integrated sounds on the sdcard.

Anyone has any pointers on this? Any help would be greatly appreciated.

Thank you.

Yanosuke
A: 

I too am looking for that answer. From what I have been able to figure out so far it may be possible to set the file "ContentValues" using "values.put(MediaStore.Audio.Media.IS_RINGTONE, true);" but I still need to know how to write that to the database itself.

NetApex
A: 

AFAIK, its not possible.

A: 

Try this, it works for me:

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, <<asbolutePathToYourAudioFileHere>>);
values.put(MediaStore.MediaColumns.TITLE, "<<yourRingToneNameHere>>");
values.put(MediaStore.MediaColumns.SIZE, k);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");    // assuming it's an mpeg, of course
values.put(MediaStore.Audio.Media.ARTIST, "<<yourArtistNameHere>>");
// values.put(MediaStore.Audio.Media.DURATION, duration);  // doesn't appear to be necessary if you don't know
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(outPath);  
Uri newUri = getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(
                                <<MyActivity>>.this,
                                RingtoneManager.TYPE_RINGTONE,
                                newUri);
Clive
A: 

Hopefully by now you have gotten your program working the way you wanted. Just for the record though, you should look into saving the file to the sdcard under a ringtones folder. Make sure it is lower cased as that does matter in Android.

NetApex
+1  A: 

@Maxood

The above code from @Clive is what you need to set the ringtone. You will need the absolute path to the file, which you can't get from a raw resource.

The solution is to get the resource file asset and write it to the sdcard 1st, before you give it to the content resolver for insertion.

File newSoundFile = new File("/sdcard/media/ringtone", "myringtone.oog");
Uri mUri = Uri.parse("android.resource://com.your.package/R.raw.your_resource_id");
ContentResolver mCr = app.getContentResolver();
AssetFileDescriptor soundFile;
try {
       soundFile= mCr.openAssetFileDescriptor(mUri, "r");
   } catch (FileNotFoundException e) {
       soundFile=null;   
   }

   try {
      byte[] readData = new byte[1024];
      FileInputStream fis = soundFile.createInputStream();
      FileOutputStream fos = new FileOutputStream(newSoundFile);
      int i = fis.read(readData);

      while (i != -1) {
        fos.write(readData, 0, i);
        i = fis.read(readData);
      }

      fos.close();
   } catch (IOException io) {
   }

Then you can use the previously posted solution

       ContentValues values = new ContentValues();
   values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
   values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
   values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/oog");
   values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
   values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
   values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
   values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
   values.put(MediaStore.Audio.Media.IS_ALARM, true);
   values.put(MediaStore.Audio.Media.IS_MUSIC, false);

   Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
   Uri newUri = mCr.insert(uri, values);


   try {
       RingtoneManager.setActualDefaultRingtoneUri(getContext(), RingtoneManager.TYPE_RINGTONE, newUri);
   } catch (Throwable t) {
       Log.d(TAG, "catch exception");
   }

hope this helps

Chiatar