1、如果读到的是音频文件路径,需要先将音乐文件插入到多媒体库。
- //设置--铃声的具体方法
- public void setMyRingtone(String path)
- {
- File sdfile = new File(path);
- ContentValues values = new ContentValues();
- values.put(MediaStore.MediaColumns.DATA, sdfile.getAbsolutePath());
- values.put(MediaStore.MediaColumns.TITLE, sdfile.getName());
- values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
- 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);
- Uri uri = MediaStore.Audio.Media.getContentUriForPath(sdfile.getAbsolutePath());
- Uri newUri = this.getContentResolver().insert(uri, values);
- RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);
- Toast.makeText( getApplicationContext (),"设置来电铃声成功!", Toast.LENGTH_SHORT ).show();
- System.out.println("setMyRingtone()-----铃声");
- }
- //设置--提示音的具体实现方法
- public void setMyNotification(String path)
- {
- File sdfile = new File(path);
- ContentValues values = new ContentValues();
- values.put(MediaStore.MediaColumns.DATA, sdfile.getAbsolutePath());
- values.put(MediaStore.MediaColumns.TITLE, sdfile.getName());
- values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
- values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
- values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
- values.put(MediaStore.Audio.Media.IS_ALARM, false);
- values.put(MediaStore.Audio.Media.IS_MUSIC, false);
- Uri uri = MediaStore.Audio.Media.getContentUriForPath(sdfile.getAbsolutePath());
- Uri newUri = this.getContentResolver().insert(uri, values);
- RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, newUri);
- Toast.makeText( getApplicationContext (),"设置通知铃声成功!", Toast.LENGTH_SHORT ).show();
- System.out.println("setMyNOTIFICATION-----提示音");
- }
- //设置--闹铃音的具体实现方法
- public void setMyAlarm(String path)
- {
- File sdfile = new File(path);
- ContentValues values = new ContentValues();
- values.put(MediaStore.MediaColumns.DATA, sdfile.getAbsolutePath());
- values.put(MediaStore.MediaColumns.TITLE, sdfile.getName());
- values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
- values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
- values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
- values.put(MediaStore.Audio.Media.IS_ALARM, true);
- values.put(MediaStore.Audio.Media.IS_MUSIC, false);
- Uri uri = MediaStore.Audio.Media.getContentUriForPath(sdfile.getAbsolutePath());
- Uri newUri = this.getContentResolver().insert(uri, values);
- RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM, newUri);
- Toast.makeText( getApplicationContext (),"设置闹钟铃声成功!", Toast.LENGTH_SHORT ).show();
- System.out.println("setMyNOTIFICATION------闹铃音");
- }
2、如果读取多媒体库的音频文件,设为铃声,使用以下方式:
首先写一个常量类(定义想要设置为那种铃声的标示):
- AppConstant.java
- public interface AppConstant {
- public static final int RINGTONE = 0; //铃声
- public static final int NOTIFICATION = 1; //通知音
- public static final int ALARM = 2; //闹钟
- public static final int ALL = 3; //所有声音
- }
此方法需要传入想要设置为铃声的全路径(如:/mnt/sdcard/mp3/a.mp3 ),和想要设置为那种铃声的标示:
- private void setVoice(String path2,int id)
- 2
- 3 {
- 4
- 5 ContentValues cv = new ContentValues();
- 6
- 7 Uri newUri = null;
- 8
- 9 Uri uri = MediaStore.Audio.Media.getContentUriForPath(path2);
- 10
- 11 // 查询音乐文件在媒体库是否存在
- 12
- 13 Cursor cursor = this.getContentResolver().query(uri, null, MediaStore.MediaColumns.DATA + "=?", new String[] { path2 },null);
- 14
- 15 if (cursor.moveToFirst() && cursor.getCount() > 0)
- 16
- 17 {
- 18
- 19 String _id = cursor.getString(0);
- 20
- 21 switch (id) {
- 22
- 23 case AppConstant.RINGTONE:
- 24
- 25 cv.put(MediaStore.Audio.Media.IS_RINGTONE, true);
- 26
- 27 cv.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
- 28
- 29 cv.put(MediaStore.Audio.Media.IS_ALARM, false);
- 30
- 31 cv.put(MediaStore.Audio.Media.IS_MUSIC, false);
- 32
- 33 break;
- 34
- 35 case AppConstant.NOTIFICATION:
- 36
- 37 cv.put(MediaStore.Audio.Media.IS_RINGTONE, false);
- 38
- 39 cv.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
- 40
- 41 cv.put(MediaStore.Audio.Media.IS_ALARM, false);
- 42
- 43 cv.put(MediaStore.Audio.Media.IS_MUSIC, false);
- 44
- 45 break;
- 46
- 47 case AppConstant.ALARM:
- 48
- 49 cv.put(MediaStore.Audio.Media.IS_RINGTONE, false);
- 50
- 51 cv.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
- 52
- 53 cv.put(MediaStore.Audio.Media.IS_ALARM, true);
- 54
- 55 cv.put(MediaStore.Audio.Media.IS_MUSIC, false);
- 56
- 57 break;
- 58
- 59 case AppConstant.ALL:
- 60
- 61 cv.put(MediaStore.Audio.Media.IS_RINGTONE, true);
- 62
- 63 cv.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
- 64
- 65 cv.put(MediaStore.Audio.Media.IS_ALARM, true);
- 66
- 67 cv.put(MediaStore.Audio.Media.IS_MUSIC, false);
- 68
- 69 break;
- 70
- 71
- 72
- 73 default:
- 74
- 75 break;
- 76
- 77 }
- 78
- 79
- 80
- 81 // 把需要设为铃声的歌曲更新铃声库
- 82
- 83 getContentResolver().update(uri, cv, MediaStore.MediaColumns.DATA + "=?",new String[] { path2 });
- 84
- 85 newUri = ContentUris.withAppendedId(uri, Long.valueOf(_id));
- 86
- 87
- 88
- 89 // 一下为关键代码:
- 90
- 91 switch (id) {
- 92
- 93 case AppConstant.RINGTONE:
- 94
- 95 RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);
- 96
- 97 break;
- 98
- 99 case AppConstant.NOTIFICATION:
- 100
- 101 RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, newUri);
- 102
- 103 break;
- 104
- 105 case AppConstant.ALARM:
- 106
- 107 RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM, newUri);
- 108
- 109 break;
- 110
- 111 case AppConstant.ALL:
- 112
- 113 RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALL, newUri);
- 114
- 115 break;
- 116
- 117
- 118
- 119 default:
- 120
- 121 break;
- 122
- 123 }
- 124
- 125
- 126
- 127 //播放铃声
- 129 // Ringtone rt = RingtoneManager.getRingtone(this, newUri);
- 131 // rt.play();
- 133 }
- 135 }