Question

I am using soundpool to play my recorded file. I just want to know that is there any way to know when soundpool finish playing. here is my working code below.

public void soundPool() {
    final float playbackSpeed = 1.5f;
    final SoundPool soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC,
            100);

    final int soundId = soundPool.load(audiofile.getAbsolutePath(), 1);
    AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    final float volume = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool arg0, int arg1, int arg2) {
            soundPool.play(soundId, volume, volume, 1, 0, playbackSpeed);
        }
    });

}
Was it helpful?

Solution 3

Yes you have to use mediaplayer for this..but using mediaplayer you can't do fast forwarding. my suggestion is you use sound pool for playing sound and before playing ,you can check time duration of sound using mediaplayer and when time wil finish you can manually stop soundpool. simple i have done this in a project.

OTHER TIPS

there is only one way that you take duration of that audio file and check it with thread.

soundPool.play(soundId, 1, 1, 1, 0, 1f);
new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {
           // do your stuff song is finished

      }
},soundDuration);

Try It.

Unfortunately there is no listener provided for telling when SoundPool has finished playing a sample. If you need this functionality, use the MediaPlayer.

As a hack, you could get the length of your audio file from an audio editor in miliseconds (X), then set a timer in you app after you tell it to play. The timer is set to fire once after X milliseconds. This way you have an approximate idea whether your sound might have finished playing. However, this is a very unreliable approach and should be avoided if it can be helped. Instead, use the MediaPlayer class with MediaPlayer.OnCompletionListener().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top