Sound, Basic: Play random sound effects

225 views
Skip to first unread message

Scott Ferguson

unread,
Sep 27, 2012, 8:46:13 AM9/27/12
to
The Sound component is ideal for adding sound effects to your project.
It is intended for short sounds of perhaps 5 seconds or less.
For longer sounds you should use the Player component.

In this exercise we will make a project that plays random sound effects using the basic functionality of the Sound component.

Create a new project named lrn_snd_basic.


Screen Designer:
Copy all six wav files to the Media list in the Screen Designer.

Change the Screen1.Title to Random sound effects.















Drag and drop a Button component onto the Viewer.
Set Button1.Text to Play.














Drag and drop a Sound component onto the Viewer.
It will appear below the Non-visible components heading.
The instance created is named Sound1.
Note also that the six sound files have been uploaded to the Media list.




















Blocks Editor:

We will create a global variable to hold our list of sound files.

Drag and drop a [def variable as] block onto the BE canvas.
Change it's name to sounds.
 - this variable will hold the list of sound effects













Attach a list from csv row block to the [def sounds as] block.
 - this block accepts text data separated by commas and converts it to a list























Attach a text block to the list from csv row block.
Change it's text to baby.wav,door.wav,heartbeat.wav,gong.wav,meow.wav,owl.wav.
 - this data will populate our list and the group of blocks will simplify operations later on





Drag and drop a Screen1.Initialize event block onto the BE canvas.
 - we will use this to pre-load all of the sound effect files before they are played (necessary to avoid an error message)











Drag and snap a foreach block into Screen1.Initialize.
 - it will be used to loop through the sounds list and initialize the sound files
  - the [name var] block will hold each file name in turn as the loop executes











Drag and click a [global sounds] variable block to the in list socket.
 - this completes the shell of the loop
 - the [global sounds] block provides the list for the foreach loop to iterate through








Drag and click a [set Sound1.Source to] block inside the foreach block.
















Drag and attach a [value var] block to the to socket of Sound1.Source.
 - the foreach loop begins with the first item in the sounds list and ends with the last item in the list
 - each time through the foreach loop, the Sound1.Source property will be set to the file name stored in the var argument








Drag and drop a Button1.Click block onto the BE canvas.
 - this block will be used to load and play a random sound effect






Drag and drop another [set Player1.Source to] block.
Snap a pick random item block to it and attach a [global sounds] block to it.
 - these blocks will pick a random sound effect file name from the sounds list and set the Sound1.Source block to that value
 - note that [set Player1.Source to] and [set sounds to] look similar as they both contain values that can be read or written, but the 'Source' variable is special in that it is always associated with a Player component



Drag and snap a Sound1.Play block below the Sound1.Source block.
 - now when the user presses the Play button (Button1) a random sound effect will play
 - (Sound1.Play looks different from Sound1.Source because it is a special procedure block that uses the value stored in Sound1.Source to play the sound effect)
 







END.

Now test your project with the emulator or device.
















Challenge: The [set Screen1.Title to] block allows us to put data in the Title bar. Can you think of a way to put the file name in Sound1.Source in the Title bar each time the user presses the Play button?
lrn_snd_basic.zip
door.wav
baby.wav
gong.wav
heartbeat.wav
meow.wav
owl.wav

M. Hossein Amerkashi

unread,
Dec 30, 2013, 9:40:39 AM12/30/13
to app-inventor-de...@googlegroups.com
Great work.
Do you have a tutorial on like a soundboard where it can record / play multiple sounds at the same time?
<p style="font: 13px/18px Arial, Helvetica, sans-serif; margin: 0px 0px 1em; padding: 0px; border: 0px currentColor; text-align: center; color: rgb(34, 34, 34); text-transform: none; text-indent: 0px; letter-spacing: normal; clear: both; word-spacing: 0px; vertical-align: baseline; white-space: normal...

Taifun

unread,
Dec 30, 2013, 9:52:51 AM12/30/13
to app-inventor-de...@googlegroups.com

Scott Ferguson

unread,
Dec 30, 2013, 10:20:41 PM12/30/13
to app-inventor-de...@googlegroups.com
As Taifun says, Gareth has an example if he will share it with you. The only way that I know to get multiple sounds to play at exactly the same time is to have an ImageSprite collide with other ImageSprites simultaneously firing their CollidedWith blocks. Inside those blocks I have the blocks to play piano note chords on my Piano Composer app/project. 
---
Scott
Reply all
Reply to author
Forward
0 new messages