Multiple reads from InputStream at different rates

I am about to upload online content (say an audio file). If I just open a connection to the remote file (for example with new URL().openStream()

) and pass the remote InputStream

to the audio player, it reads gradually from the network. If the library of the audio player does not ask for more data InputStream

, it does not read anything from the network, and then when the library demands more, it reads again.

My problem is that I want to start caching the next online audio file as soon as the download process of the first one is complete. Using normal InputStream

, the read completes as the audio plays, which is not very good as I want it to read faster if the network bandwidth allows it, so caching the next audio file. My audio files are less than 3MB, so they can be safely cached into memory. I only prefetch one file, so no memory leak problems.

Is there some type of caching InputStream

that also allows simultaneous reads? Then I can start the stream and cache the audio faster than the audio player is consuming data.

+2


source to share


1 answer


I would recommend creating a second thread to perform the download. Download files as fast as possible in this stream and move the data to the main playback stream.



+2


source







All Articles