我正在尝试将默认通知声音设置为原始目录中的.mp3文件。当我设置它时,它会正确显示文件的名称,但不会播放声音。我做错了什么?
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();
这是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
我认为问题在于它正在/internal/audio/media/
中寻找文件,但它应该在/res/raw/
中查找。我该如何改变?
我试图以其他方式设置默认通知声音。第一种方式设置声音,但名称显示为mp3 id。第二种方式显示正确的名称,但不播放任何声音。这是我的代码:
//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)
返回/res/raw/
中mp3文件的id,names.get(pos)
返回我想要显示的名称。如何将名称设置为u1
,或者u2
为什么不播放任何声音?使用u2
时,我得到了这个logcat:
E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present
E/MediaPlayer: error (1, -2147483648)
D/Ringtone: Problem opening; delegating to remote player