以下代码(不是我真正写的)用于通过inputStream将wav文件的数据写入android的AudioTrack
(对我的问题不重要...)
int bytesread = 0, ret = 0;
int size = (int) file.length();
at.play();
while (bytesread < size) {
ret = in.read(byteData, 0, count);
if (ret != -1) { // Write the byte array to the track
at.write(byteData, 0, ret);
bytesread += ret;
}
}
如果您使用 java 的 File
类,您可以使用 File.length()
来检查文件的大小并查看何时停止流式传输/播放。但是我已经将我的音频文件存储在 android Resources (R.raw.example)
中并打开它我使用:
in = mResources.openRawResource(ResId);
这给了我一个 InputStream
。这使我无法使用类似于 File.length()
的任何内容,因此我不知道文件何时播放。
有谁知道如何实现与第一个示例类似的东西?
最佳答案
尝试 openRawResourceFd .它应该返回 AssetFileDescriptor ,它有一个 .getLength() 方法。
关于File.length() 的 Android 资源替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7364958/