【问题标题】:Android Resources alternative for File.length()?File.length() 的 Android 资源替代方案?
【发布时间】:2011-11-13 23:08:14
【问题描述】:

以下代码(不是我真正编写的)用于通过 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() 的任何东西,因此我不知道文件何时播放。

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

【问题讨论】:

    标签: android file resources io inputstream


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-05-18
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      相关资源
      最近更新 更多