Soundpool plays only first 5 secs of file. Why?

无人久伴 提交于 2019-11-27 20:37:04

So I think you reached the 1M limit in SoundPool.

SoundPool is hard code the buffer size as 1M, for all loaded file, store in pcm format.

So it do not care of ogg or wav.

We have solved similar problem by decreasing in our *.ogg effects sample rate. Our initial sample rate was 44 kHz ~ and only 10 sec of sound played, decreasing to 16 kHz increase playability to 30 seconds

Solution was found in this discussion

SoundPool designed to play short sound effects. To play music (big audio files) you need to use MediaPlayer

// R.raw.audio_file - res/raw/audio_file.mp3

mediaPlayer = MediaPlayer.create(this, R.raw.audio_file);
mediaPlayer.start();

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

Your file is probably too large. Try to change from stereo to mono, if you do not need stereo anyway. Or try to downsample your file.

Reducing the filesice worked in my case.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!