6

I have two issues I would like understand.

I am using soundPool for my sound effects and its working with no problem. However, when I try to play cetain file (25 sec , about 400K) it doesn't play the whole file only 3-4 seconds from it .

Why and how can I fix it ?

and the second question is, should I play each effect from thread ? many threads are good ?

this is the current code :

static void play(final int soundID ){
    if(loaded){
        handler.post(new Runnable()
        {
        public void run()
        {
            soundpool.play(soundID, 1, 1, 1, 0, 1);
        }
        });
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Jesus Dimrix
  • 4,378
  • 4
  • 28
  • 62
  • Check the return of the `play` method. From the spec: `Returns non-zero streamID if successful, zero if failed` – Voicu Jun 13 '13 at 22:14
  • return 1 . and as i said it does play...for 3-4 sec – Jesus Dimrix Jun 13 '13 at 22:53
  • Yes, but it could have been failing after 3-4 seconds of playing. Does it return after 3-4 seconds or after 25 seconds? – Voicu Jun 13 '13 at 22:55
  • didnt understand what to check , it return the streamId as the playing start... – Jesus Dimrix Jun 13 '13 at 22:58
  • I'd take `soundpool.play(soundID, 1, 1, 1, 0, 1);` out of that `Runnable` (so it falls right under the if statement), see if that works or not. – Voicu Jun 13 '13 at 23:00
  • i tryied this way after playing without thread did the same problem . no change – Jesus Dimrix Jun 13 '13 at 23:08
  • 1
    Try this instead of the soundpool play statement: `MediaPlayer.create(YourActivity.this, R.raw.your_sound).start();`, see if that plays the full sound. – Voicu Jun 13 '13 at 23:14
  • this works , ill use it if i wont fix the problem with soundpool . i try to avoid using MediaPlayer for sound that can be played number of times . – Jesus Dimrix Jun 13 '13 at 23:30

1 Answers1

5

My understanding is that SoundPool cannot be used for sounds longer than several seconds or audio files >1MB. Use MediaPlayer in those cases.

Either use this for each play of a sound:

MediaPlayer.create(YourActivity.this, R.raw.your_sound).start();

or create MediaPlayer object, play same sound as many times as needed, then release() the object.

Voicu
  • 16,921
  • 10
  • 60
  • 69
  • Mediaplayer cant be used more than once without side effects, most time, previously mediaplayer's got ignored – payam_sbr Mar 09 '17 at 04:21