2011-08-21 62 views
1

我有一个soundboard应用程序,我使用SoundPool来控制播放我的声音。一切都很好,直到我尝试添加大约30秒钟的声音。当我点击该声音的按钮时,它只会播放几秒钟然后停止。有没有一种方法可以防止使用SoundPool发生这种情况?这是我的代码。谢谢!如何防止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); 
} 
} 

回答

0

它仅用于播放短音,请阅读SDK页面。

0

SoundPool的内部存储器限制为1(一)Mb。 如果你的声音质量很高,你可能会打这个。 如果您有很多声音并正在达到此限制,只需创建更多声音库,然后将声音分散到它们中间。

0

的Soundpool是一个轻量级的选手,将只解码第一1Mb的数据,但如果你想使用更优质的或长的音频文件,你需要:

* 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

有一些的Soundpool信息我拿起开发我的音乐应用程序。下面是一些没有涉及的原始文件基础上的限制与声音的长度有一定的指导线:

Length of Sound    Khz 
--------------------  --------------- 
    1-5 Secs.    44khz - Stereo 
    6-11 Secs.    22khz - Stereo 
    12-21 Secs.    11khz - Mono 
    21+ Secs.    8khz - Mono 

所以,如果您有过得去的Soundpool切断任何音频文件,转换用这个。当然,当转换到11khz或更低时,音频开始听起来有点低质量,但总比没有好。我用来转换的程序是:免费MP3 WMA OGG转换器。在处理Soundpool时我总是使用ogg文件。希望这帮助人们。