Android SoundPool 堆限制
我正在使用 SoundPool 加载多个声音剪辑并播放它们。
据我所知,它的功能 100% 正确。但是在 .load() 调用期间,我的日志中充斥着以下内容:
06-09 11:30:26.110: ERROR/AudioCache(23363): Heap size overflow! req size: 1050624, max size: 1048576
我正在加载 11 个声音文件,其中 2 个非常小~3kb,其余的在 10kb - 15kb 之间。 Windows 报告
大小:114kb
磁盘大小:128kb
我是否突破了 SoundPool 能够容纳的极限?我是否应该更改一些设置以避免这种溢出?任何有关我应该如何设置音频控件的指导将不胜感激。
I am using a SoundPool to load several sound clips into and play them back.
It is functioning 100% correctly from what I can tell. But during the .load() calls I am getting my log spammed with:
06-09 11:30:26.110: ERROR/AudioCache(23363): Heap size overflow! req size: 1050624, max size: 1048576
I am loading in 11 sound files, of those 2 are very small ~3kb and the rest are between 10kb - 15kb. Windows is reporting
Size: 114kb
Size on disk: 128kb
Am I pushing the limits of what SoundPool is capable of holding? Is there some setting I should be altering to avoid this overflow? Any guidance on how I should be setting up my audio controls would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SoundPool 会将加载的音频解压缩为 PCM 数据,以便立即播放,没有解码延迟。如果您正在加载的音频被严重压缩,例如 MP3,那么它可能会被放大很多。如果不需要立体声(通常不适用于短音效),请尝试将音频编码为单声道。
SoundPool is going to decompress the loaded audio to PCM data so it is ready to play instantly without the latency of decoding. If the audio you are loading is compressed heavily, such as MP3, then that can get blown up quite a bit. Try encoding the audio as mono if stereo isn't necessary (it usually isn't for short sound effects).
SoundPool
每个轨道的缓冲区大小限制为 1Mb。但此限制不适用于文件大小,而是适用于解压缩的原始 PCM 数据。我建议使用 3d 方库中的
SoundPoolCompat
。它具有与SoundPool
类似的 api,但具有自定义缓冲区大小,该缓冲区内的所有数据都将加载到内存中,并像SoundPool
一样以较小的延迟播放。所有超过该缓冲区大小的数据都将按需加载(这会增加延迟,类似于 MediaPlayer)。但它不会崩溃,也不会像 SoundPool 那样播放仅适合缓冲区大小的数据。此外,缓冲区大小以文件大小表示,而不是解压缩数据大小。这对api用户来说更方便。
https://gitlab.com/olekdia/common/libraries/sound-pool
SoundPool
has 1Mb buffer size limit per track. But this limit applies not to file size but to decompressed raw PCM data.I suggest to use
SoundPoolCompat
from 3d party library. It has similar api toSoundPool
, but has custom buffer size, all data within that buffer will be loaded into memory and played with small latency likeSoundPool
does. All data that exceed that buffer size will be loaded on demand (which adds latency, similar toMediaPlayer
). But it will not crash nor play data that's only fits buffer size like SoundPool.Also buffer size is expressed in file size not in decompressed data size. Which is more convenient to api user.
https://gitlab.com/olekdia/common/libraries/sound-pool