File.length()的Android资源替代方案?

时间:2011-09-09 16:53:30

标签: android file resources io inputstream

以下代码(不是我真正编写的)用于通过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()检查文件的大小,并查看何时停止流式传输/播放。但是我已将我的audiofiles存储在android Resources (R.raw.example)中并打开它我使用:

 in = mResources.openRawResource(ResId);  

给了我一个InputStream。这使我无法使用类似于File.length()的任何内容,因此我不知道该文件何时播放。

有谁知道如何实现与第一个例子类似的东西?

1 个答案:

答案 0 :(得分:3)

试试openRawResourceFd。它应该返回一个AssetFileDescriptor,它有一个.getLength()方法。