一、若是讀到的是音頻文件路徑,須要先將音樂文件插入到多媒體庫。java
- //設置--鈴聲的具體方法
- 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------鬧鈴音");
- }
二、若是讀取多媒體庫的音頻文件,設爲鈴聲,使用如下方式:ide
首先寫一個常量類(定義想要設置爲那種鈴聲的標示):this
- 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 ),和想要設置爲那種鈴聲的標示:spa
- 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 }