Soundpool plays only first 5 secs of file. Why?

前端 未结 5 1607
独厮守ぢ
独厮守ぢ 2020-12-05 14:04

I use Soundpool in my app, so far it works good, but I do have a wav file which is 10 secs. Unfortunately, soundpool plays only the first 5 secs. How to make soundpool to pl

5条回答
  •  隐瞒了意图╮
    2020-12-05 14:47

    This activity is works for me

    public class MainActivity extends Activity {
    
        private Integer soundID;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
            // Load the sound
            AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
            float actualVolume = (float) audioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
            float maxVolume = (float) audioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            final float volume = actualVolume / maxVolume;
    
            SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    
            soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
              @Override
              public void onLoadComplete(SoundPool soundPool, int sampleId,
                  int status) {
                          @Override
          public void onLoadComplete(SoundPool soundPool, int sampleId,
              int status) {
              soundPool.play(soundID, volume, volume, 1, 0, 1f);
              try {
                Thread.sleep(5000); // play twice
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
              soundPool.play(soundID, volume, volume, 1, 0, 1f);
          }
        });
              }
            });
            soundID = soundPool.load(this, R.raw.sound2, 1);
          //load fx
          //playing soundpool
    
        }
    }
    

    You must load you sound asynchrony and check audio cache - it can be overflow.

    Read this article http://www.google.by/url?http://www.vogella.com/articles/AndroidMedia/article.html

    Its very heedful

提交回复
热议问题