Friday, June 3, 2011

More exercises on App Inventor

In the last post, i talked about getting my first android app "Hellopurr" to work. It was really a breeze.
While playing with some ideas, a colleague asked if a short sound clip could be made to sound repeatedly when a button is pressed, and to stop playing the clip when the same button is pressed again.
Writing an "If-then-else" statement in the when.button.click event causes the button to be stuck in an endless loop. The "meow" from the cat kept going on and on non-stop and it triggered some uninvited comments from the boss! I had to quickly reboot the phone to get the "meow" out from the loop. Gosh!

Did some googling and found that the problem can be solved with a button and a clock component.

I don't think i can explain that well enough in words. I have done a screen capture of the blocks editor:

















a. A boolean variable "sound" is defined.
b. Under the "do" section of  "when Button1.Click" component, invert the variable "sound". This achieves the toggling function of the button. 
c. Under the "do" section of "when Clock1.Timer" component, a "if-then-else" statement is used. Basically, what it does is, to check the state of the variable "sound". If it is "true", then play the sound clip, if it is "false", then stop it.
d. The clock component will repeat itself every 1000 milliseconds.

No comments:

Post a Comment