简体   繁体   中英

Writing file on Android with Okio

Hi All I'm reading raw audio from Android device and I want to write it into a file using Okio like this:

BufferedSink sink = Okio.buffer(Okio.sink(file));

I have a callback to retrieve audio data, and then write the audio data to bufferedSink

@Override
public void onAudioData(byte[] data) {
    sink.write(data);
}

But sometimes, when I listen to the audio file the last frames are missing, and all files have a length of 8182 bytes or multiples.

If I use FileOutputStream everything works well, but this is a first approach and my future purpose is to stream the audio using Okio Pipes and OkHttp. Does anyone know why this is happening?

Your code doesn't show the full usage of sink , but it seems like you're not closing it when you're done with it. Because of that, only those internal segments (which are 8192 bytes long, so your file size should be a multiple of that number) that are full gets written to the disk, while the last one doesn't. As a solution you should close() your Sink .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM