Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Soundpool plays only first 5 secs of file. Why?

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 play the whole track? I have converted wav to -- ogg and mp3 still the same issue. It plays only the first 5 secs. Any help would be much appreciated.

//set up audio player
mSoundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
//load fx
mSoundPoolMap.put(RAW_1_1, mSoundPool.load(this, R.raw.loop1, 1));
//playing soundpool
case R.id.button1:          
mSoundPool.stop(mStream1);
mStream1= mSoundPool.play(mSoundPoolMap.get(RAW_1_1), streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);

UPD Last: Maybe someone will find it here and read it. Seems soundpool cant play more then 5 secs. It is his maximum, for more longer sounds use MediaPlayer. I hope you will not spend so much of your time like i did)

like image 573
Daler Avatar asked Nov 14 '12 11:11

Daler


4 Answers

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.

like image 143
ifreedom Avatar answered Nov 10 '22 01:11

ifreedom


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

like image 39
Gennadiy Ryabkin Avatar answered Nov 10 '22 01:11

Gennadiy Ryabkin


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();
like image 26
Vadim Zin4uk Avatar answered Nov 10 '22 00:11

Vadim Zin4uk


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

like image 21
Yahor10 Avatar answered Nov 10 '22 01:11

Yahor10