Reputation: 707
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 example?
Upvotes: 1
Views: 573
Reputation: 515
Try openRawResourceFd. It should return an AssetFileDescriptor, which has a .getLength() method.
Upvotes: 3