发布网友 发布时间:2022-04-26 22:18
共1个回答
热心网友 时间:2022-06-15 12:47
我们在拿到新手机后通常会为其设置来年铃声,那么怎样通过代码来设置Android来电铃声,本文就为大家实例讲解下。
1、如果读到的是音频文件路径,需要先将音乐文件插入到多媒体库。
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------闹铃音");
}
2、如果读取多媒体库的音频文件,设为铃声,使用以下方式:
首先写一个常量类(定义想要设置为那种铃声的标示):
AppConstant.java
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),和想要设置为哪种铃声的标示:
Java代码
private void setVoice(String path2,int id)
{
ContentValues cv = new ContentValues();
Uri newUri = null;
Uri uri = MediaStore.Audio.Media.getContentUriForPath(path2);
// 查询音乐文件在媒体库是否存在
Cursor cursor = this.getContentResolver().query(uri, null, MediaStore.MediaColumns.DATA + "=?", new String[] { path2 },null);
if (cursor.moveToFirst() && cursor.getCount() > 0)
{
String _id = cursor.getString(0);
switch (id) {
case AppConstant.RINGTONE:
cv.put(MediaStore.Audio.Media.IS_RINGTONE, true);
cv.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
cv.put(MediaStore.Audio.Media.IS_ALARM, false);
cv.put(MediaStore.Audio.Media.IS_MUSIC, false);
break;
case AppConstant.NOTIFICATION:
cv.put(MediaStore.Audio.Media.IS_RINGTONE, false);
cv.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
cv.put(MediaStore.Audio.Media.IS_ALARM, false);
cv.put(MediaStore.Audio.Media.IS_MUSIC, false);
break;
case AppConstant.ALARM:
cv.put(MediaStore.Audio.Media.IS_RINGTONE, false);
cv.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
cv.put(MediaStore.Audio.Media.IS_ALARM, true);
cv.put(MediaStore.Audio.Media.IS_MUSIC, false);
break;
case AppConstant.ALL:
cv.put(MediaStore.Audio.Media.IS_RINGTONE, true);
cv.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
cv.put(MediaStore.Audio.Media.IS_ALARM, true);
cv.put(MediaStore.Audio.Media.IS_MUSIC, false);
break;
default:
break;
}
// 把需要设为铃声的歌曲更新铃声库
getContentResolver().update(uri, cv, MediaStore.MediaColumns.DATA + "=?",new String[] { path2 });
newUri = ContentUris.withAppendedId(uri, Long.valueOf(_id));
// 一下为关键代码:
switch (id) {
case AppConstant.RINGTONE:
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);
break;
case AppConstant.NOTIFICATION:
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, newUri);
break;
case AppConstant.ALARM:
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM, newUri);
break;
case AppConstant.ALL:
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALL, newUri);
break;
default:
break;
}
//播放铃声
//Ringtone rt = RingtoneManager.getRingtone(this, newUri);
//rt.play();
}
}