开发者

Android Resources alternative for File.length()?

开发者 https://www.devze.com 2023-04-04 08:04 出处:网络
The following code (not really written by me) is used to write data of a wav file through an inputStream to android\'s AudioTrack (unimportant for my question...)

The following code (not really written by me) is used to write data of a wav file through an inputStream to android's AudioTrack (unimportant for my question...)

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;
    }
}

If you use java's File class, you can use File.length() to check the file's size and see when to stop streaming/playing. However I have stored my audiofiles in the android Resources (R.raw.example) and to open it I use :

 in = mResources.openRawResource(ResId);  

which gives me an InputStream. This makes me unable to use anything similar to File.length() and therefore I don't know when the file has been played.

Does anyone know how to achieve something similar as the first exam开发者_开发问答ple?


Try openRawResourceFd. It should return an AssetFileDescriptor, which has a .getLength() method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号