以下代码(不是我真正写的)用于通过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()来检查文件的大小,看看什么时候停止流/播放。然而,我已经将我的音频文件存储在安卓Resources (R.raw.example)中,要打开它,我使用:
in = mResources.openRawResource(ResId); 这给了我一个InputStream。这使得我无法使用任何类似于File.length()的东西,因此我不知道文件何时播放。
有没有人知道如何实现与第一个示例类似的东西?
发布于 2011-09-10 01:02:13
试试openRawResourceFd。它应该返回一个具有.getLength()方法的AssetFileDescriptor。
https://stackoverflow.com/questions/7364958
复制相似问题