gpt4 book ai didi

Android 设置铃声只能在第一次使用

转载 作者:行者123 更新时间:2023-11-29 00:38:12 25 4
gpt4 key购买 nike

我的 android 应用程序有一个小问题。我的应用程序中有一些 mp3 文件,我希望能够将其设置为默认铃声或通知。我按照这篇文章的说明进行操作 Setting Ringtone in Android它有点管用。

这是我的代码:

创建要设置为铃声的文件:

public static String createFile(String raw, int choice){
File newSoundFile = null;
String path = (choice == 0) ? "/mnt/sdcard/media/mySoundsRingtone.mp3" : "/mnt/sdcard/media/mySoundsNotification.mp3";
newSoundFile = new File(path);
if(newSoundFile.exists()){
try {
newSoundFile.delete();
} catch (Exception e) {
Toast.makeText(cont, "delete failed", Toast.LENGTH_SHORT).show();
return null;
}

}

try {
newSoundFile.createNewFile();
} catch (IOException e) {
Toast.makeText(cont, "file creation failed", Toast.LENGTH_SHORT).show();
return null;
}
//Toast.makeText(cont, "file created", Toast.LENGTH_SHORT).show();



Uri mUri = Uri.parse("android.resource://my.package.name/"+raw);

ContentResolver mCr = cont.getContentResolver();
AssetFileDescriptor soundFile;
try {
soundFile= mCr.openAssetFileDescriptor(mUri, "r");
} catch (FileNotFoundException e) {
Toast.makeText(cont, "AssetFileDescriptor failed", Toast.LENGTH_SHORT).show();
soundFile=null;
return null;
}

try {
byte[] readData = new byte[1024];
FileInputStream fis = soundFile.createInputStream();
FileOutputStream fos = new FileOutputStream(newSoundFile);
int i = fis.read(readData);

while (i != -1) {
fos.write(readData, 0, i);
i = fis.read(readData);
}

fos.close();
} catch (Exception io) {
Toast.makeText(cont, "copy failed", Toast.LENGTH_SHORT).show();
return null;
}

return path;
}

设置文件为铃声:

public static void setValues(String path, int choice){
File newSoundFile = new File(path);

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
if(choice == 0){
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
}
else{
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(newSoundFile.getAbsolutePath());
Uri newUri = mCr.insert(uri, values);


try {
if(choice == 0)RingtoneManager.setActualDefaultRingtoneUri(cont, RingtoneManager.TYPE_RINGTONE, newUri);
else RingtoneManager.setActualDefaultRingtoneUri(cont, RingtoneManager.TYPE_NOTIFICATION, newUri);
} catch (Throwable t) {
Log.d("LOG", "catch exception");
}


}

同时调用:

public static void setRingTone(String raw, int choice){
setValues(createFile(raw, choice), choice);


}

所以发生的事情是:我第一次选择铃声时,新文件被创建,声音被正确指定为铃声。如果我再次单击以选择铃声,旧的铃声文件将被删除并正确重新创建,但它不会设置为铃声(当我被调用时,我只会收到振动)。但是,如果我手动删除 .mp3 文件并转到我的应用程序并再次选择铃声,它会起作用。为什么当我从我的代码中删除该文件时它的行为与从文件浏览器中手动删除它时的行为不同?

任何想法可能是什么问题?

提前致谢!

最佳答案

好的,经过几个小时的搜索,我在这篇文章中找到了答案:setting audio file as Ringtone

(我不知道如何将我的问题标记为重复)

关于Android 设置铃声只能在第一次使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11278481/

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