0

I am wondering if it's possible to write an application that would randomly pick audio files (ringtones) from a predefined/configurable list and play it when I receive a call. So my ringtone would change even when I am called by the same person consecutively.

Is this possible and if so how complicated would it be? Would you need to take dependency on provider or OS into account?.

4

1 回答 1

1

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

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

于 2013-06-18T14:05:32.117 回答