5

I'm making a soundboard application and am trying to have a sound play as a ringtone or notification depending on which menu item is clicked. The ringtone currently shows up as the default ringtone in the ringtone menu, but does not play when a phone call comes in. What am I doing wrong? My code is listed below.

public boolean saveas(int ressound,String file,String typesound){
    byte[] buffer=null;  
    InputStream fIn = getBaseContext().getResources().openRawResource(ressound);  
    int size=0;  

    try {  
        size = fIn.available();  
        buffer = new byte[size];  
        fIn.read(buffer);  
        fIn.close();  
    } catch (IOException e) {  
        // TODO Auto-generated catch block  
        return false;  
    }  

    String path="/sdcard/media/audio/ringtones/";  
    String filename=file+".ogg";  

    boolean exists = (new File(path)).exists();  
    if (!exists){new File(path).mkdirs();}  

    FileOutputStream save;  
    try {  
        save = new FileOutputStream(path+filename);  
        save.write(buffer);  
        save.flush();  
        save.close();  
    } catch (FileNotFoundException e) {  
        // TODO Auto-generated catch block  
        return false;  
    } catch (IOException e) {  
        // TODO Auto-generated catch block  
        return false;  
    }      

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));  

    File k = new File(path, filename);  

    ContentValues values = new ContentValues();  
    values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
    values.put(MediaStore.MediaColumns.TITLE, file);  
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
    values.put(MediaStore.Audio.Media.ARTIST, "douchebag");  
    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);  

    //Insert it into the database  
    this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);  

    //set ringtone
    Uri ringtoneUri = Uri.parse(path+filename);
    if(typesound=="ringtone")
        RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, ringtoneUri);

    return true;  
}  
jhoward
  • 404
  • 1
  • 6
  • 16
  • Do you get an error? What is the behaviour you are seeing? Also, try logging when you catch an exception if it cause your app to not function as expected. – CrackerJack9 Aug 12 '11 at 15:51

3 Answers3

1

According to this question here, you need to set the ringtone with the "external" URI. As far as I can tell, you basically have all the pieces and just need to make a tiny adjustment.

Try if this snippet works for you:

//Insert it into the database
Uri ringtoneUri = this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values); 
//set ringtone    
if(typesound=="ringtone")
    RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, ringtoneUri);
Community
  • 1
  • 1
Junibert
  • 46
  • 6
0
FullBatteryAlarm.prefs = PreferenceManager
            .getDefaultSharedPreferences(this);
    String strRingtonePrefs = FullBatteryAlarm.prefs.getString(
            FullBatteryAlarm.res.getString(R.string.ringtone), "content://settings/system/alarm_alert");
    //Log.i("strringtone", strRingtonePrefs);
    Uri notification = Uri.parse(strRingtonePrefs);

    AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    //mgr.getStreamMaxVolume(AudioManager.STREAM_ALARM);
    mgr.setStreamVolume(
            AudioManager.STREAM_RING, 
            mgr.getStreamMaxVolume(AudioManager.STREAM_RING),
            AudioManager.FLAG_PLAY_SOUND);
    //Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();

OR you can get your default notification sound.

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    r.play();
Engineer
  • 49
  • 3
0

Hey, I think you need to set the permission in the android manifest. Below is the permission I used.

 <uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>

Also I'm not sure about ogg files. I used mp3 files and it worked for me.

Derek MC
  • 366
  • 2
  • 4
  • 25