views:

5556

answers:

6

Hi

Im trying to find a way to set a new default ringtone by code from my Android activity.

I have already downloaded the ringtone into a bytearray.

Anybody got a hint?

A: 

You can use the built-in RingtonePreference class. AndroidGuys has a nice tutorial on this here.

russoue
Uh, thanks for the shout-out, but RingtonePreference allows the user to choose a ringtone asset for *your* application to use. It does not set the system ringtone.
CommonsWare
Ok, may be I missed but the reference does not explicitly tell that. I wonder what would someone do with a ringtone in his application unless the app is handling incoming calls.
russoue
A: 

I still have not found the solution. Im trying with this code, but it does not work. But it does play file.

Uri ringtoneUri = Uri.parse("/sdcard/media/audio/ringtones/myringtone.mp3");
rm.setActualDefaultRingtoneUri(main, rm.TYPE_RINGTONE, ringtoneUri);
Ringtone rt = rm.getRingtone(main,ringtoneUri);
rt.play();

This code only start playing the ringtone, so rt.play() does work. But the setActualDefaultRingtoneUri() does not. No exceptions or warnings in the ddms.

PHP_Jedi
+6  A: 

Finally, I managed to set the default ringtone to one that i downloaded. The download code is not included below, only what was needed to set it as default ringtone.

File k = new File(path, "mysong.mp3"); // path is a file to /sdcard/media/ringtone

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.SIZE, 215454);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
values.put(MediaStore.Audio.Media.DURATION, 230);
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 uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = main.getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(
  myActivity,
  RingtoneManager.TYPE_RINGTONE,
  newUri
);

Anyway, I do not totally understand what this code is doing.

The Ringtone manager needs a uri to the file that is to be set as new ringtone. But this uri can not be directly to the sdcard like "/sdcard/media/ringtones/mysong.mp3". That does not work!

What you need is the external file uri of the file which could be something like "/external/audio/media/46"

The 46 is the id of the column in the MediaStore database, so thats why you need to add the sdcard file into the database first.

Anyway, how does mediastore maintain its ids? This number can get really high, as you do this operation many times.

Do i need to delete this row my self? Problem is that some times i dont even controll the deleting of the file since it can be deleted directly from the sdcard with a filebrowser.

PHP_Jedi
+1  A: 

Hi, I found this code from the Media application from Android.

 Settings.System.putString(resolver, 
   Settings.System.RINGTONE, ringUri.toString());

this works form my,

Regards andrefy

Andres Yajamin
A: 

I really dont get where i should add the code you mention, please could you reply again with what the SoundBoardTest.java code and SoundManager.java code should actually be, (with 10 sounds, sound1, sound2 etcc..)

Many thanks

lucy