I am trying to set the default notification sound to a .mp3 file from the raw directory. When I set it, it shows the name of the file correctly but no sound plays. What am I doing wrong?
File f = new File("android.resource://" + context.getPackageName()
+ "/" + files.get(pos));
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, names.get(pos));
values.put(MediaStore.MediaColumns.SIZE, f.length());
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
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 u = MediaStore.Audio.Media.getContentUriForPath(f.getAbsolutePath());
context.getContentResolver().delete(
u, MediaStore.MediaColumns.DATA + "=\""
+ f.getAbsolutePath() + "\"", null);
final Uri uri = context.getContentResolver().insert(u, values);
Log.d("URI", uri.toString() + "\n"
+ uri.getPath() + "\n"
+ uri.getEncodedPath());
RingtoneManager.setActualDefaultRingtoneUri(context,
RingtoneManager.TYPE_NOTIFICATION,
uri);
RingtoneManager.getRingtone(context, uri).play();
This is the logcat:
D/URI: content://media/internal/audio/media/158
/internal/audio/media/158
/internal/audio/media/158
E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present
D/MediaPlayer: Couldn't open file on client side, trying server side
E/MediaPlayer: Unable to create media player
D/Ringtone: Problem opening; delegating to remote player
I think the problem is that it is looking for the file in /internal/audio/media/
but it should be looking in /res/raw/
. How do I change this?
I have tried to set the default notification sound 2 other ways. The first way sets the sound, but the name displayed it the mp3 id. The second way displays the right name, but does not play any sound. This is my code:
//no name, sound
final Uri u1 = Uri.parse("android.resource://" + context.getPackageName() + "/" + files.get(pos));
//no sound, name
File file = new File("android.resource://" + context.getPackageName() + "/" + files.get(pos), names.get(pos));
final Uri u2 = Uri.fromFile(file);
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION, u1);
RingtoneManager.getRingtone(context, u1).play();
files.get(pos)
returns the id of the mp3 file in /res/raw/
and names.get(pos)
returns the name I want to display. How can I set the name to u1
, or why will u2
not play any sound? When using u2
I get this logcat:
E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present
E/MediaPlayer: error (1, -2147483648)
D/Ringtone: Problem opening; delegating to remote player