以不同速率从 InputStream 进行多次读取

发布于 2024-08-07 11:27:22 字数 484 浏览 2 评论 0原文

我即将加载在线内容(例如音频文件)。如果我只是打开到远程文件的连接(例如通过使用 new URL().openStream() )并将远程 InputStream 传递给音频播放器,则它从网络逐渐读取。如果音频播放器库不向 InputStream 请求更多数据,它不会从网络读取任何数据,然后当库请求更多数据时,它会再次读取。

我的问题是,我想在第一个在线音频文件的加载过程完成后立即开始缓存下一个在线音频文件。使用普通的InputStream,随着音频​​播放完成,读取就完成了,这不好,因为我希望在网络带宽允许的情况下读取得更快,以便缓存下一个音频文件。我的音频文件小于 3mb,因此可以安全地缓存到内存中。我只预取一个文件,所以没有内存泄漏的问题。

是否有任何类型的缓存InputStream,它也允许并发读取?然后,我可以运行一个线程并以比音频播放器消耗数据的速度更快的速度缓存音频。

I'm about to load an online content (say an audio file). If I just open a connection to the remote file (for example by use of new URL().openStream()) and pass the remote InputStream to the audio player, it reads gradually from the network. If audio player library do not ask InputStream for more data, it reads nothing from the network, and then when the library claims more, it reads again.

My problem is that I want to start caching the next online audio file, as soon as the loading process of the first one is finished. Using a normal InputStream, reading is complete as the audio playing is done, which is not good since I want it read faster if network bandwidth allows, so that caching the next audio file. My audio files are smaller that 3mb so can be safely cached to the memory. I only prefetch a single file, so no problem with memory leak.

Is there any type of caching InputStream, which also allows concurrent read? I can then run a thread and cache the audio faster than the audio player consumes the data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2024-08-14 11:27:22

我建议您创建第二个线程来执行下载。在此线程中尽快下载文件并将数据推送到主播放线程。

I would recommend that you create a second thread to perform the downloading. Download files as quickly as possible in this thread and push the data over to your main playback thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文