永远在Android中暂停播放

时间:2012-05-26 11:52:47

标签: android audio soundpool

我有问题。当我尝试使用Soundpool播放声音时,每次播放之间会有一个小的暂停。 例如,按下按钮时我想播放2秒声音。 我尝试用相同的声音开始2个线程。第二次开始后0.5秒。但没有任何帮助。

    AudioManager  mAudioManager;
    SoundPool mSoundPool;
    Runnable thr1;
    Runnable thr2;

    public void playLoopedSound(int soundId) {
        mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
        mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)*2; 
        thr1 = new Runnable() {
            public void run() {
                mSoundPool.play(soundId, 0, streamVolume, 1, -1, 1f); //0 = no loop, -1 = loop forever
            }
        };


   thr2 = new Runnable() {
        public void run() {
            mSoundPool.play(soundId, 0, streamVolume, 1, -1, 1f); 
        }
    };
    thr1.run();
    try {
        Thread.sleep(400);
    } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
        thr2.run();
    }

AudioManager mAudioManager; SoundPool mSoundPool; Runnable thr1; Runnable thr2; public void playLoopedSound(int soundId) { mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0); mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE); int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)*2; thr1 = new Runnable() { public void run() { mSoundPool.play(soundId, 0, streamVolume, 1, -1, 1f); //0 = no loop, -1 = loop forever } }; thr2 = new Runnable() { public void run() { mSoundPool.play(soundId, 0, streamVolume, 1, -1, 1f); } }; thr1.run(); try { Thread.sleep(400); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } thr2.run(); }

1 个答案:

答案 0 :(得分:0)

很难在没有调试的情况下知道,但是由于soundpool在加载声音时的表现,我遇到了类似的问题。为了解决这个问题,我实现了SoundPool.onLoadCompleteListener以确保在声池完全加载之前我没有开始尝试播放声音。

请注意,这仅适用于API8或更高版本。请参阅此答案以获得一个很好的实现:

Knowing if the loading of a sound with SoundPool has been successful on Android 1.6/2.0/2.1