android 添加音乐作为铃声

File k = new File(path, "mysong.mp3");
// path is /sdcard/media/ringtone
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.SIZE, 215454);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
values.put(MediaStore.Audio.Media.DURATION, 230);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the databaseUri
uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = main.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(  myActivity,  RingtoneManager.TYPE_RINGTONE,  newUri);

播放
Uri ringtoneUri = Uri.parse("/sdcard/media/audio/ringtones/myringtone.mp3");
rm.setActualDefaultRingtoneUri(main, rm.TYPE_RINGTONE, ringtoneUri);
Ringtone rt = rm.getRingtone(main,ringtoneUri);
rt.play();

你可能感兴趣的:(android,音乐)