【问题标题】:Forever playing without pause in Android在 Android 中永远不间断地播放
【发布时间】:2012-05-26 20:03:10
【问题描述】:

我有问题。当我尝试使用 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();
    }

【问题讨论】:

    标签: android audio soundpool


    【解决方案1】:

    如果不进行调试很难知道,但由于 soundpool 在加载声音时的性能,我遇到了类似的问题。为了解决这个问题,我实现了 SoundPool.onLoadCompleteListener 以确保在 soundpool 完全加载之前我不会开始尝试播放声音。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2017-08-15
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      相关资源
      最近更新 更多