2

i am having a small problem with my android application. I have some mp3 files in my app which i want to be able to set as default ringtone or notification. I followed the instructions on this post Setting Ringtone in Android and it kind of works.

here is my code:

Creating the file to set as ringtone:

public static String createFile(String raw, int choice){
    File newSoundFile = null;
    String path = (choice == 0) ? "/mnt/sdcard/media/mySoundsRingtone.mp3" : "/mnt/sdcard/media/mySoundsNotification.mp3";
    newSoundFile = new File(path);
    if(newSoundFile.exists()){
        try {
            newSoundFile.delete();
        } catch (Exception e) {
            Toast.makeText(cont, "delete failed", Toast.LENGTH_SHORT).show();
            return null;
        }

    }

    try {
        newSoundFile.createNewFile();
    } catch (IOException e) {
        Toast.makeText(cont, "file creation failed", Toast.LENGTH_SHORT).show();
        return null;
    }
    //Toast.makeText(cont, "file created", Toast.LENGTH_SHORT).show();



    Uri mUri = Uri.parse("android.resource://my.package.name/"+raw);

    ContentResolver mCr = cont.getContentResolver();
    AssetFileDescriptor soundFile;
    try {
           soundFile= mCr.openAssetFileDescriptor(mUri, "r");
       } catch (FileNotFoundException e) {
           Toast.makeText(cont, "AssetFileDescriptor failed", Toast.LENGTH_SHORT).show();
           soundFile=null; 
           return 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 (Exception io) {
           Toast.makeText(cont, "copy failed", Toast.LENGTH_SHORT).show();
           return null;
       }

    return path;
}

Setting the file as ringtone:

public static void setValues(String path, int choice){
    File newSoundFile = new File(path);

    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/mp3");
       values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
       values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
       if(choice == 0){
           values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
           values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
       }
       else{
           values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
           values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
       }
       values.put(MediaStore.Audio.Media.IS_ALARM, false);
       values.put(MediaStore.Audio.Media.IS_MUSIC, false);

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


       try {
           if(choice == 0)RingtoneManager.setActualDefaultRingtoneUri(cont, RingtoneManager.TYPE_RINGTONE, newUri);
           else RingtoneManager.setActualDefaultRingtoneUri(cont, RingtoneManager.TYPE_NOTIFICATION, newUri);
       } catch (Throwable t) {
           Log.d("LOG", "catch exception");
       }


}

Calling both:

public static void setRingTone(String raw, int choice){
    setValues(createFile(raw, choice), choice);


}

so what happens is: the first time that i select a ringtone the new file is created, and the sound is assigned properly as the ringtone. If I click again to select a ringtone the old ringtone file is deleted and recreated properly, but it in not set as a ringtone (when i get called i get only vibration). BUT if I delete the .mp3 file manually and go to my app and select ringtone again it works.. Why does it behave differently when i delete the file from my code than when it is deleted manually from a file browser?

Any ideas what might be the problem?

Thanks in advance!

Community
  • 1
  • 1
tk66
  • 274
  • 7
  • 20
  • possible duplicate of [Setting Ringtone in Android](http://stackoverflow.com/questions/1986756/setting-ringtone-in-android) – sshashank124 Apr 20 '14 at 15:33

2 Answers2

2

Ok after a few hours of searching i found the answer on this post: setting audio file as Ringtone

(i don't know how to mark my question as a duplicate)

Community
  • 1
  • 1
tk66
  • 274
  • 7
  • 20
0

For me its not working in first time but it works from second time onwards

Uri ruri = Uri.parse("content://media/internal/audio/media/37");

try 

{

RingtoneManager.setActualDefaultRingtoneUri(context,RingtoneManager.TYPE_RINGTONE,     
ruri);

}

catch (Throwable t) 

{

// error handling goes here -- also, use something other than Throwable

}

AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

for (int volumeType: (new int[] { AudioManager.STREAM_SYSTEM,

AudioManager.STREAM_RING, AudioManager.STREAM_NOTIFICATION, AudioManager.STREAM_ALARM   
}))

{

int maxVolume = audio.getStreamMaxVolume(volumeType);


audio.setStreamVolume(volumeType, maxVolume,AudioManager.FLAG_PLAY_SOUND |     
AudioManager.FLAG_VIBRATE);

audio.setStreamMute(volumeType, false);

 audio.setVibrateSetting(volumeType,AudioManager.VIBRATE_SETTING_ON);

 }

 }

 catch (Exception e) 
 {

 // TODO: handle exception

 }

 }
Community
  • 1
  • 1
AndroidRaji
  • 907
  • 14
  • 26