gpt4 book ai didi

以编程方式设置Android铃声?

转载 作者:行者123 更新时间:2023-11-29 14:47:52 25 4
gpt4 key购买 nike

我想在录制语音后为我的应用程序设置铃声。铃声只会正确设置一次,它将被设置为铃声,而再次设置它不能正常工作,我已经添加了我的代码:

String filepath ="/sdcard/sample/"+currentName+"";
System.out.println("/sdcard/sample/"+currentName+"");


File ringtoneFile = new File(filepath);

ContentValues content = new ContentValues();
content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
content.put(MediaStore.MediaColumns.TITLE, currentName);
content.put(MediaStore.MediaColumns.SIZE, 215454);
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
// content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
content.put(MediaStore.Audio.Media.DURATION, 230);
content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
content.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
content.put(MediaStore.Audio.Media.IS_ALARM, true);
content.put(MediaStore.Audio.Media.IS_MUSIC, true);

String Ringtonepath= "content://media/internal/audio/media/297";
Uri Ringtone1 = Uri.parse(Ringtonepath);
//Insert it into the database
Log.i("TAG", "the absolute path of the file is :"+
ringtoneFile.getAbsolutePath());
Uri uri = MediaStore.Audio.Media.getContentUriForPath(
ringtoneFile.getAbsolutePath());
Uri newUri = getContentResolver().insert(uri, content);
System.out.println("uri=="+uri);

Log.i("TAG","the ringtone uri is :"+newUri);
// getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"",
// null);

RingtoneManager.setActualDefaultRingtoneUri(
getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
newUri);

我的错误是:

新铃声:

TAG    the ringtone uri is :content://media/internal/audio/media/297

现有铃声:

TAG    the ringtone uri is :null

最佳答案

重新排列代码行..据我了解,您在删除旧铃声之前插入了新铃声。只需将上面的代码替换为这个。

 String filepath ="/sdcard/sample/"+currentName+"";
System.out.println("/sdcard/sample/"+currentName+"");


File ringtoneFile = new File(filepath);

ContentValues content = new ContentValues();
content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
content.put(MediaStore.MediaColumns.TITLE, currentName);
content.put(MediaStore.MediaColumns.SIZE, 215454);
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
// content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
content.put(MediaStore.Audio.Media.DURATION, 230);
content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
content.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
content.put(MediaStore.Audio.Media.IS_ALARM, true);
content.put(MediaStore.Audio.Media.IS_MUSIC, true);

String Ringtonepath= "content://media/internal/audio/media/297";
Uri Ringtone1 = Uri.parse(filepath);
//Insert it into the database
Log.i("TAG", "the absolute path of the file is :"+
ringtoneFile.getAbsolutePath());
Uri uri = MediaStore.Audio.Media.getContentUriForPath(
ringtoneFile.getAbsolutePath());




getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"",
null);
Uri newUri = getContentResolver().insert(uri, content);
System.out.println("uri=="+uri);
Log.i("TAG","the ringtone uri is :"+newUri);
RingtoneManager.setActualDefaultRingtoneUri(
getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
newUri);

关于以编程方式设置Android铃声?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29256684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com