Trouble with sound and a repeat loop

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
DougN
Posts: 20
Joined: Wed Mar 04, 2009 4:09 am

Trouble with sound and a repeat loop

Post by DougN » Tue Jun 14, 2011 8:55 am

Hi,

I'm having a heck of a time working with playing basic sounds in a simple (at this point) 2 card stack. My goal is to move to a card, and play a sound file that asks the user to click on an object. The sound file will repeat every five seconds until the user clicks the object. If they don't get it the first time, I show an image of a finger touching the object so they get a visual clue (that also repeats).

Image

Here's my code:

Code: Select all

on opencard
     wait 1 second
     play (specialfolderpath("engine") & "/sounds/" & thelanguage & "thesoundfile.mp3") with messages
     repeat while touched = "false"
      wait 5 seconds with messages
      play (specialfolderpath("engine") & "/sounds/" & thelanguage & "thesoundfile.mp3") with messages
      show image "touching"
      wait 1 second
      hide image "touching"
   end repeat
end opencard
The problem is that once a sound file plays, execution stops. I've figured this out because if I comment out the "play" commands:

Code: Select all

   
repeat while touched = "false"
      wait 5 seconds with messages
      -- play (specialfolderpath("engine") & "/sounds/" & thelanguage & "thesoundfile.mp3") with messages
      show image "touching"
      wait 1 second
      hide image "touching"
 end repeat 
it happily repeats the hide / show image forever (well, OK, I haven't tested it for that long).

The global variable "touched" gets set to true by the button on the graphic that they need to touch.

Any thoughts, comments, etc. are welcome. With humble thanks in advance, Doug

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3990
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Trouble with sound and a repeat loop

Post by bn » Tue Jun 14, 2011 9:48 am

Hi Doug,

I suspect there is an error that halts the execution of the remaining script.

try

Code: Select all

play (specialfolderpath("engine") & "/sounds/" & thelanguage & "thesoundfile.mp3")
without "with messages"
In iOS scripts fail silently unless you provide a means of catching the error.

Try to add to your card script:

Code: Select all

on errorDialog
put the params into field "fErr"
end errorDialog
and you add a field named "fErr" to your card. The error is then displayed in the field. It is not very informative but at least you know there is an error.

I often do this to test a stack in iOS. I put above script into the stack script and have a background group with the field "fErr" on every card.

Alternatively you could open Console and see the error there when in the simulator. Or if on a real device you could see the errors in Xcode if the device is connected to Xcode.

Kind regards

Bernd

DougN
Posts: 20
Joined: Wed Mar 04, 2009 4:09 am

Re: Trouble with sound and a repeat loop

Post by DougN » Tue Jun 14, 2011 10:32 am

Hi Bernd,

Thanks very much for the quick and helpful reply. Indeed, when I remove the "with messages" from the "play" command, it plays the sound correctly. So that issue is taken care of.

I'm still not able to figure out how to accomplish what I want on the card -- which is that the sound prompt is played, and then it repeats every five seconds until they touch an object. What happens now is that they touch the object, but it still waits, and then plays the sound prompt again.

I guess I need to look for a better way to construct this repeat loop, perhaps without the "wait" feature? Or is there a command in the table object so that I can use when they touch the table it not only goes to the next card, but also stops the wait / play process on the current one?

Best,
Doug

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3990
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Trouble with sound and a repeat loop

Post by bn » Tue Jun 14, 2011 10:52 am

Hi Doug,

you do need the wait with messages in the repeat loop, in fact you could add a "with messages" to the wait 1 second. If you omit the wait it will repeat too fast. If you omit the "with messages" then Livecode is blocked and user input is not possible.
I don't see that you initialized the global variable "touched". If that is so that would explain what you see.

You can either initialize it in the opencard handler like:

Code: Select all

on opencard
     global touched
     -- the rest of you code
end opencard
or

Code: Select all

global touched
on opencard
  -- your code
end opencard
the difference being that in the first version only the opencard handler sees the global touched, in the second version every handler in that script can use the global touched.

Kind regards

Bernd

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Trouble with sound and a repeat loop

Post by jacque » Tue Jun 14, 2011 4:50 pm

DougN wrote:I'm still not able to figure out how to accomplish what I want on the card -- which is that the sound prompt is played, and then it repeats every five seconds until they touch an object. What happens now is that they touch the object, but it still waits, and then plays the sound prompt again.
You should use a "send in <time>" structure instead of a wait. A wait will hang up everything until it is done, as you found out. Basically you set a flag that stores whether or not your loop should execute. Then, if the flag is true, you send a message every few seconds that triggers your sound. This method is preferable not only for the reasons you noticed, but because it is much easier on the CPU and the system on the whole.

There is an explanation here, and while it deals with mouse polling, the concept applies to all kinds of looping behavior:
<http://www.hyperactivesw.com/polling.html>

If you need help with the actual script, let us know.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “iOS Deployment”