2011-08-21 104 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文件。希望這幫助人們。