1

我有一个音板应用程序,我使用 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);
}
}
4

4 回答 4

1

我在开发我的音乐应用程序时收集了一些 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 或更低时,音频开始听起来有点低质量,但总比没有好。我用来转换的程序是:Free MP3 WMA OGG Converter。在处理 Soundpool 时,我总是使用 ogg 文件。希望这对人们有所帮助。

于 2014-09-12T05:16:03.513 回答
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.
于 2013-11-30T19:22:47.557 回答
0

SoundPool 的内部内存限制为 1(一)Mb。如果您的声音质量非常高,您可能会遇到这个问题。如果你有很多声音并且达到了这个限制,只需创建更多的声音池,并在它们之间分配你的声音。

于 2013-10-08T00:15:19.093 回答
-1

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

于 2011-11-18T03:14:03.987 回答