Kamis, 11 Mei 2023

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
soundpool
else
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:


Share:

2 komentar:

Alam terbuka mengasyikkan di Destinasi impian

Jasa Pembuatan Website Archives - Legenda Web

loading...

Archive

Cari Blog Ini

Diberdayakan oleh Blogger.

Mengenai Saya

Foto saya
Saya memiliki Jasa Pembuatan Website Terpercaya
www.legendaweb.com

Pernah mengerjakan seo untuk Agen Travel door to door ecopremieretransindo.com dan Agen Travel Jakarta Jogja jakjogtrans.com

Juga memegang SEO Biro jasa paspor

Selain Itu, kerjasama dengan Sewa Mobil Bali Mai Bali Trans dan Industri pariwisata trip Bali dari beautrip.id

Labels

Admob ads in Sketchware Admob Ads in Sketchware App admob ads using AIDE Admob banner ads using AIDE Admob interstitial Ads Android app Android change status bar color animation in Sketchware background music Change color of checkbox in Sketchware change color of Seekbar change color of Switch button change status bar color code for moving image with finger in android code Injection create and share text file in Sketchware create text file in Sketchware Custom listview in android Custom ListView in Sketchware CustomView in Sketchware Dictionary Android App Dictionary Android project Dictionary App Dictionary App in Sketchware drag image enable download from Sketchware enable download in webview enable download without code Injection Enable image upload enable upload from webview enable upload from webview in android enable upload in Sketchware enable upload in webview​ using AIDE enable upload using AIDE gif image in Android App gif image in webview how to use ListView in Sketchware how to use Spinner image zoom Integrate Admob Ads in Sketchware Intent large paragraphs in Sketchware larger texts in Edittext. limit input text length load image in webview make image fit screen make scrollbar invisible media player move image with finger notification icon in Android notification in Android Notifications in Sketchware open other apps retrieve APK file retrieve source code share button share button in Android App share button in Sketchware App Share text file Sketchware sound on off sound play pause soundpool Soundpool for Multiple sounds Sounds in Android App stopwatch Android App Stopwatch App string list timer use of list