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

0

SoundPool の内部メモリは 1 Mb に制限されています。サウンドが非常に高品質な場合は、これに該当する可能性があります。多数のサウンドがあり、この制限に達している場合は、さらにサウンドプールを作成し、それらにサウンドを分散させてください。

于 2013-10-08T00:15:19.093 に答える
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 に答える