Difference Between SoundPool and MediaPlayer

Add sounds to your Android applications!

Sung Park
Sketchware
5 min readMar 3, 2017

--

What’s new?

Sketchware’s new major update (version 1.2.0) provides a new component that everybody has been waiting for — sound. Two new components were added: SoundPool and MediaPlayer. In this article, I’ll explain the difference between the two, and elaborate when you should use one over another.

Sounds! 🔊

First of all, you need to know that SoundPool and MediaPlayer can both handle sound. Here is a detailed explanation of two components:

SoundPool

A SoundPool is a collection of samples that can be loaded into memory from a resource inside the APK or from a file in the file system, which means it is designed for short clips and it is best suited for sound effects.

MediaPlayer

MediaPlayer is designed for longer sound files or streams, this is best suited for music files or larger files. The files will be loaded from disk each time create is called, this will save on memory space but introduce a small delay (not really noticeable).

StealthCopter

Simply put, use SoundPool for short sounds, and use MediaPlayer for any long background music.

Playing Sounds in Sketchware

Adding Sounds

A new menu called “Sound Manager” has been added. Just like the Image Manager, this is where you can add local sound files. Both MediaPlayer and SoundPool search here for available sounds.

SoundPool

First off, in order to access the blocks for SoundPool, you need to add the component under the Components tab.

1. create max stream count — we first need to initialize how many sounds the pool can hold.

2. load — loads the sound file and creates a soundID

3. play — plays the sound with the soundID of x. By default it plays one time; the second argument repeats the sound for y times. If you pass “-1” for the second argument, the sound will play infinitely until you stop.

4. stop streamID — stops the stream with the streamID of x.

Think of SoundPool as a pool of sounds (hence SoundPool). I’ve explained how SoundPool works behind the scenes below:

  1. First, an empty pool that can hold x number of streams needs to be created.
  2. When you load a sound, the pool creates an arbitrary soundID. You need a number variable that can hold reference to the soundID (This is necessary for the next step).
  3. When you play a sound using the soundID, it creates an arbitrary streamID. Create a number variable that can hold reference to the streamID (streamID is needed to stop the sound).

Here is a basic example of how you should be using soundPool to play sounds. Note that the play block is not a command block, or a block that performs an action. By setting the stream variable to the SoundPool’s play block, it both plays the sound and assigns the streamID.

Warning

If you try to play a long sound file using SoundPool, you might notice that the sound gets cut off. This is due to the limitation of memory SoundPool can allocate. For any long sounds, you need to use MediaPlayer component.

MediaPlayer

Just like SoundPool, add MediaPlayer under the Components tab to gain access to the blocks. Here are the blocks that come with MediaPlayer. Note that I stacked the command blocks just for display purposes:

MediaPlayer blocks

1. Create — one MediaPlayer can play one sound at a time. You can re-use the MediaPlayer if you overwrite the MediaPlayer with a new sound file

2. Start — starts the sound it was created with

3. Pause — pauses the sound

4. Seek to — seeks to the given time in milliseconds

5. Set looping — setting the looping to true loops the sound once the sound is finished

6. Reset — resets the sound to the beginning of the file, back to 0:00.

7. Release — releases the sound. Once released, it has no access to the sound file anymore unless you re-create it

8. is looping — true if looping, else false

9. is playing — true if playing, else false

10. get current duration — gets the current position of the sound.

11. get song duration — gets the duration of the sound file.

This would be a very basic example to create and play a sound using MediaPlayer. The empty parameter would be the name of the sound you added.

Warning

Even if you exit out of the app, the music will continue to play. This is because the app is still running behind the scenes. If you want to stop the music when you exit the app, you can check out the new Lifecycle events we added in this update. Add a new onStop event, and simply release the MediaPlayer there.

Conclusion

Today, we’ve talked about the difference between SoundPool and MediaPlayer. These are some key points you should take away from this article:

  1. Use SoundPool for short sound clips, and use MediaPlayer for any long sound clips.
  2. Know the difference between reset and release on MediaPlayer.
  3. New Lifecycle events were added.

That’s it! Happy Coding :-)

If you enjoyed this post, please press the 💚 below so others can find this article too!

--

--