Play/pause button for mediaplayer and soundpool

Sometimes the user is playing your game in an office or in a classroom, he may like to switch off the game sounds. And sometimes he may like to switch off sounds to save battery. So it is important to have to a play/pause button for the sounds in your app.

To use a mediaplayer for background sound in Sketchware, in onCreate event use MediaPlayer create, set looping and start blocks.
For using soundpool, add a number variable 'sound'. Then use soundpool create max stream count (5) (*if you have five different sounds). Then one by one add the five sounds to the number variable 'sound' using the block soundID...load...
And then wherever the sound is to be played, set number variable 'sound' to StreamID SoundPool name play sound ID ... 1+ times.

Now, in order to create a play/pause button for both the Mediaplayer and the soundpool together, follow the steps given below.

1. Add an Imageview and two images, one for sound on and other for sound off.

2. Create a new block named soundpool and to it add Soundpool create max stream count 5. Then load all sounds to the number variable 'sound', same as what was done in onCreate event.
3. Add a new number variable named 'backsound'.

4. Then in onCreate event, replace all blocks related to soundpool with the new block soundpool. Also set number variable 'backsound' to 1, and set image of Imageview to sound on image.
 5. Now add event Imageview (the Imageview being used as on/off button for sound) onClick and add the following blocks:
When imageview50 clicked
If backsound = 0 then
set Number backsound to 1
Imageview setImage (Volume on image)
MediaPlayer set looping true
MediaPlayer start
If backsound = 1 then
set Number backsound to 0
Imageview setImage (Volume off image)
MediaPlayer pause
MediaPlayer set looping false
SoundPool create max stream count 0.

The video below explains it better:


Popular posts from this blog

Simple car racing android game in Sketchware

Enable Fullscreen for Youtube videos in WebView

Creating a Drawing View in Sketchware

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware