Now you can Subscribe using RSS

Submit your Email

Thursday

Android's SoundPool not playing any sound, sample not ready | FIX

Vishal Shrestha
The SoundPool is a very useful class for playing and managing audio resources in Android applications. While using SoundPool class is fairly easy, there are  two different constructors that we can use to initialize the SoundPool class, one is for API version less than 21, and one is for API version 21 and greater. You can check both of them out here - Android SoundPool Constructor deprecated.

Soundpool-not-playing-sound-android
SoundPool Class Android

Most common problems as to why the sound player is not playing any sound are :

  1. SoundPool sample 1 not Ready problem.
  2. Before SoundPool can be used, we need to make sure the resources are properly loaded and soundpool is ready to be use. Otherwise when you call the play method, you will get the sampe 1 not Ready message and you won't hear anything played. To fix this just use onLoadComplete listener and play the sound in side the onLoadComplete callback. Here's how you use it:
    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                @Override
                public void onLoadComplete(SoundPool soundPool, int sampleId,
                        int status) {
                    soundPool.play(yourresID, 1, 1, 1, 0, 1);
                }
            });

  3. Not passing proper resourse ID to the play method of the SoundPool class.
  4. In order to play sound from a resource you need to pass the ID that is generated using the load method of soundPool Class, here's an example :
    int mysound = soundPool.load(this, R.raw.mysound, 1);
    soundPool.play(mysound, 1, 1, 1, 0, 1);

That's all, hope your problem is fixed and you can play sounds using SoundPool, check out the new constructor for SoundPool class here - Android SoundPool Constructor deprecated.
If you have any problems, feel free to message me or comment your problem down below! Cheers.

Vishal Shrestha / Author & Founder

A developer by profession, a born Adventurer. I mainly do Android but like to get my hands dirty with web development and a little bit of Python. I would't rather go on a Trek than a party and you can find me having a few rounds with the heavy bag to let out the steam ;)

For Business info : My Portfolio Site.

0 comments:

Post a Comment

Coprights @ 2017 | The Code City by Vishal Shrestha Vishal Shrestha