gpt4 book ai didi

android - 创建的铃声文件没有任何声音

转载 作者:太空狗 更新时间:2023-10-29 13:25:25 28 4
gpt4 key购买 nike

我让我的应用程序从 Raw 文件夹中获取文件并将该文件设置为 Ringtone。但是有一个问题,文件被创建并设置为铃声:http://prntscr.com/2so80e但是文件没有任何声音,我不知道为什么我在默认情况下猜测我的设备正在播放另一个铃声。这是我的代码:

case 64:        
String path = "android.resource://" + getPackageName() + "/"+R.raw.fusrodah;
File k= new File(path);
Log.i("OUTPUT", path);

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k .getPath());
values.put(MediaStore.MediaColumns.TITLE, "Fusrodah File");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
values.put(MediaStore.Audio.Media.ARTIST, "Testing");
values.put(MediaStore.MediaColumns.SIZE, 215454);
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(k.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(Context.this,
RingtoneManager.TYPE_RINGTONE, newUri);

break;

我做错了什么?有什么我想念的吗?我有所有权限,文件已创建但没有任何声音。

最佳答案

看来您应该先将文件复制到 SD 卡,然后将此副本用作铃声。这里有完整的代码示例(我的 Assets 中有文件“kalimba.mp3”):

private int size;

private static final int BUFFER_LEN = 1024;

private void copyFile(AssetManager assetManager, String fileName, File out) throws FileNotFoundException, IOException {
size = 0;
FileOutputStream fos = new FileOutputStream(out);
InputStream is = assetManager.open(fileName);
int read = 0;
byte[] buffer = new byte[BUFFER_LEN];
while ((read = is.read(buffer, 0, BUFFER_LEN)) >= 0) {
fos.write(buffer, 0, read);
size += read;
}
fos.flush();
fos.close();
is.close();
}

@Override
public void onClick(View arg0) {
AssetManager assetManager = getAssets();

File file = new File(Environment.getExternalStorageDirectory(),
"/myRingtonFolder/Audio/");
if (!file.exists()) {
file.mkdirs();
}

String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/myRingtonFolder/Audio/";

File out = new File(path + "/", "kalimba.mp3");
if(!out.exists()){
try {
copyFile(assetManager, "kalimba.mp3", out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, out.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "name");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, out.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, true);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(out.getAbsolutePath());
ContentResolver mCr = getContentResolver();
Uri newUri = mCr.insert(uri, values);

try {
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);
Settings.System.putString(mCr, Settings.System.RINGTONE,
newUri.toString());
}
catch (Throwable t)
{
//TODO Handle exception
}

}

关于android - 创建的铃声文件没有任何声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21797997/

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