I'm doing my first app: I want to put a button in order to set a .mp3 as a ringtone, everythig works ok, Logcat an eclipse doens't show any kind of error, but when I make a call (in emulator and real mobile phone) it doesn't sound! also, when I go to "Music" app and try to play those ringtones (I find them in "recently added") it showsme this: "Sorry, the player does not support type of audio file".
What I'm doing wrong?
Here is my code:
setringtone.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast msg = Toast.makeText(TonosSet.this, "Sound set as ringtone!", Toast.LENGTH_LONG);
msg.show();
File k = new File("/sdcard/media/ringtone", "song_file.mp3");
Uri mUri = Uri.parse("android.resource://com.app/"+R.raw.song_file);
ContentResolver mCr = getContentResolver();
AssetFileDescriptor soundFile;
try {
soundFile= mCr.openAssetFileDescriptor(mUri, "r");
} catch (FileNotFoundException e) {
soundFile=null;
}
try {
InputStream ins = TonosSet.this .getResources().openRawResource (R.raw.song_file);
byte[] buffer = new byte[ins.available()];
ins.read(buffer);
ins.close();
String filename = Environment.getExternalStorageDirectory().toString()+File.separator+R.raw.song_file;
FileOutputStream fos = new FileOutputStream(filename);
fos.write(buffer);
fos.close();
} catch (IOException io) {
}
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Name sound");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Artist");
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
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
TonosSet.this,
RingtoneManager.TYPE_NOTIFICATION,
newUri);
}
});
Please, help me, I don't know what to do, I've been looking for a solution for about 5 hours but I can't =(