如何防止 SoundPool 截断声音
我有一个音板应用程序,我使用 SoundPool 来控制声音的播放。一切都很顺利,直到我尝试添加大约 30 秒长的声音。当我单击该声音的按钮时,它只播放几秒钟然后停止。有没有办法使用 SoundPool 来防止这种情况发生?这是我的代码。谢谢!
public class SoundManager {
private SoundPool mSoundPool;
private HashMap<Integer, Integer> mSoundPoolMap;
private AudioManager mAudioManager;
private Context mContext;
public SoundManager()
{
}
public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void addSound(int Index,int SoundID)
{
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}
public void playSound(int index)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}
public void playLoopedSound(int index)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
}
}
I have a soundboard app and I use SoundPool to control the playback of my sounds. Everything worked great until I tried to add a sound that was about 30 seconds long. When I click the button for that sound it only plays for a few seconds then stops. Is there a way to prevent this from happening using SoundPool? Here is my code. Thanks!
public class SoundManager {
private SoundPool mSoundPool;
private HashMap<Integer, Integer> mSoundPoolMap;
private AudioManager mAudioManager;
private Context mContext;
public SoundManager()
{
}
public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void addSound(int Index,int SoundID)
{
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}
public void playSound(int index)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}
public void playLoopedSound(int index)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在开发音乐应用程序时收集了一些 Soundpool 信息。以下是一些基于声音长度限制的原始文档中未涵盖的指南:
因此,如果您有任何被 SoundPool 截断的音频文件,请使用它进行转换。当然,当转换为 11khz 或更低时,音频听起来质量有点低,但总比没有好。我用来转换的程序是:Free MP3 WMA OGG Converter。在处理 Soundpool 时我总是使用 ogg 文件。希望这对大家有帮助。
There are some Soundpool information I pick up while developing my music app. Here are some guide lines that not covered in the original documentation base on limitations with length of sound:
So if you have any audio files that are getting cut off by SoundPool, convert using this. Of course when converting to 11khz or below, the audio start to sound a bit low quality, but better than nothing. The program I use to convert is: Free MP3 WMA OGG Converter. I always use ogg files when dealing with Soundpool. Hope this help folks.
SoundPool 的内部内存限制为 1(一)Mb。
如果您的声音质量非常高,您可能会遇到这个问题。
如果您有很多声音并且达到了此限制,只需创建更多声音池,并将声音分布在它们之间即可。
SoundPool has an internal memory limitation of 1 (one) Mb.
You might be hitting this if your sound is very high quality.
If you have many sounds and are hitting this limit, just create more soundpools, and distribute your sounds across them.
SoundPool 是一个轻量级播放器,它只会解码前 1Mb 数据,但如果您想使用更高质量或更长的音频文件,您需要:
SoundPool is a lightweight player which will only decode the first 1Mb data, but if you want to use a more high quality or long audio file, you need to:
它仅用于播放短声音,请阅读 SDK 页面。
it is only meant for playing short sounds, read the SDK page.