gpt4 book ai didi

java - 如何读取音频文件

转载 作者:行者123 更新时间:2023-12-03 01:45:53 26 4
gpt4 key购买 nike

在一个Android项目中,我正在将音频文件读取为InputStream,然后将其写入设备上的另一个位置。阅读时,如片段中所示的in.read(buffer)返回-1。运行该应用程序后,我使用手机的文件管理器应用程序定位该文件以进行播放。但由于文件为空,即文件大小为0 bytes,因此无法播放。使用InputStreamsOutputStreams读写音频文件的正确方法是什么?

try {
DocumentFile newFile = pickedDir.createFile("audio/mp3", "New File");
OutputStream out = getContentResolver().openOutputStream(newFile.getUri());
InputStream in = new FileInputStream("/storage/emulated/0/beat.mp3");
// To check whether the source file exists
File testFile = File("/storage/emulated/0/beat.mp3");
Log.d("App", "exists: " + testFile.exists() + " len: " + testFile.length());//exists: true len: 0
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
out.flush();
out.close();
} catch (Exception e) {
Log.d("Exception ", e.getMessage());
}

最佳答案

要学究,使用:

FileInputStream in = new FileInputStream("/storage/emulated/0/beat.mp3");

并确保路径正确并且文件存在。除此之外,从您提供的信息中,我再也想不到

关于java - 如何读取音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43766389/

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