首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何防止SoundPool截断声音

问如何防止SoundPool截断声音
EN

Stack Overflow用户
提问于 2011-08-22 07:32:54
回答 4查看 902关注 0票数 1

我有一个soundboard应用程序,我使用SoundPool来控制我的声音的回放。一切都很好,直到我尝试添加一个大约30秒长的声音。当我点击那个声音的按钮时,它只播放了几秒钟,然后就停止了。有没有办法通过使用SoundPool来防止这种情况发生?这是我的代码。谢谢!

代码语言:javascript
复制
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);
}
}
EN

回答 4

Stack Overflow用户

发布于 2014-09-12 13:16:04

有一些我在开发音乐应用程序时收集到的Soundpool信息。以下是基于声音长度限制的原始文档中未涵盖的一些指南:

代码语言:javascript
复制
   Length of Sound              Khz
--------------------      ---------------
     1-5 Secs.             44khz - Stereo
    6-11 Secs.             22khz - Stereo
   12-21 Secs.             11khz - Mono
     21+ Secs.              8khz - Mono 

因此,如果你有任何音频文件被SoundPool截断,使用这个进行转换。当然,当转换到11 the或更低时,音频开始听起来有点低质量,但总比没有好。我用来转换的程序是:免费MP3 WMA OGG转换器。在处理Soundpool时,我总是使用ogg文件。希望这对大家有帮助。

票数 1
EN

Stack Overflow用户

发布于 2013-10-08 08:15:19

SoundPool的内存限制为1(1) Mb。如果你的声音质量很高,你可能会碰到这个。如果你有很多声音,并且达到了这个限制,只需创建更多的声音池,并将你的声音分布在它们之间。

票数 0
EN

Stack Overflow用户

发布于 2013-12-01 03:22:48

SoundPool是一个轻量级播放器,它只会解码前1Mb的数据,但如果您想使用更高质量或更长的音频文件,您需要:

代码语言:javascript
复制
* Use MediaPlayer interface instead of SoundPool. MediaPlayer is more complicated mechanism so the playback startup time longer than SoundPool.
* Use MediaCodec API to decode data firstly then call AudioTrack API to playback PCM data as requested.This solution will decode the data firstly then do PCM playback directly, so the delay will be the same as SoundPool but it's more complicated for programming.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7141863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档