Simple Sound Loop

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
db221
Posts: 4
Joined: Mon Sep 24, 2012 4:14 pm

Simple Sound Loop

Post by db221 » Fri Sep 28, 2012 4:06 pm

Hi Guys
Very new to LiveCode and trying to get used to the language style of it; coming from a C background.

Have a simple app using MobGui plugin. essentially have four option buttons labeled as 5, 10, 15, 20. User selects option, then selects play and it should play a sound x number of times based on the option button pressed.

In my card script I have
***********
global gLoopsToPlay
***********

in my option button script I have
************
on mouseDown
if the environment = "development" then touchStart 1
put uOptionText into gLoopsToPlay
end mouseDown
************

and in my play sound button script I have
************
on mouseUp
if the environment = "development" then touchEnd 1
local tLoopCount
Put 0 into tLoopCount
repeat while tLoopCount < gLoopsToPlay
play specialFolderPath("engine") & "/sound.caf"
Put tLoopCount + 1 into tLoopCount
end repeat
end mouseUp
***********

Livecode just hangs, as does app in iPhone simulator, suspect this is because it is getting into a infinte loop. Can anyone suggest what I have done wrong and how I can get this working as intended?

monte
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1564
Joined: Fri Jan 13, 2012 1:47 am
Contact:

Re: Simple Sound Loop

Post by monte » Sat Sep 29, 2012 1:10 am

Hmm... Have you implemented touchStart and touchEnd? What's in the uOptionText variable and where is it initialized? Also you might want to queue the sounds using the multi-channel sound commands.
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7390
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Simple Sound Loop

Post by jacque » Sat Sep 29, 2012 6:53 am

There are a few things to change and then it will work. But first a caveat: in this case you probably don't want to implement both mouseDown/touchStart and mouseUp/touchEnd because the way the script is written you'll get mouse events happening twice on both a device and in the IDE. The easiest way to handle that is to use only mouseDown and mouseUp handlers, since those will work everywhere. Don't bother writing touch handlers for simple button presses. The touch events are mostly useful for multi-touch apps.

Assuming there are no touchStart or touchEnd handlers written, then nothing will happen on a device when touch events occur. In the IDE, you should be getting script errors when you call "touchStart 1" and "touchEnd 1"; the engine should complain it can't find the handler. Check to see if you have script debugging mode turned on in the Debug menu that appears when the script editor is open. Most of us never turn that off.

The parameters you're sending with the touch messages aren't usually script-assigned; they are assigned by the engine on a real device and they allow tracking multi-touch events like pinch or zoom. The parameter isn't needed for a single button press, though it won't hurt anything. (But if you don't bother with touch handlers at all and just use mouse handlers, then it's moot anyway.)

As Monte mentioned, the uOptionText variable doesn't seem to have a value. Unless you've declared it as a script local and have given it a value somewhere else, it will be empty. That means gLoopsToPlay will also be empty. When comparing numbers, empty and zero are equivalent, so the repeat loop will never run and nothing will play.

Some of us use the "u" designator to indicate a custom property. If you are trying to put the value of a custom property into the variable, then the syntax is something like this: put the uOptionText of button x into gLoopsToPlay

You're off to a good start, so with a few modifications it will work. Keep us posted and ask if you need more info.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Klaus
Posts: 14182
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Simple Sound Loop

Post by Klaus » Sun Sep 30, 2012 2:26 pm

Hi db221,

problem is that "play" is NOT blocking, so you should not hear any sound at all 8)
You could try this:

Code: Select all

...
repeat gLoopsToPlay
    play specialFolderPath("engine") & "/sound.caf"
    ## Now wait for the sound to finish
    wait until the sound = "done"
  end repeat
...
Best

Klaus

db221
Posts: 4
Joined: Mon Sep 24, 2012 4:14 pm

Re: Simple Sound Loop

Post by db221 » Tue Oct 02, 2012 5:44 pm

Thanks for those pointers people. I think this is as much of a learning to different approach that LiveCode uses as much as anything, your pointers have got be onto a better track.

@monte - Yes touchStart and touchEnd are implemented, only to house a mobGUI line of code
example
on touchStart pId
mobGUITouch the long id of me
end touchStart

The uOptionText variable is a mobGUI system variable containing the text of the option button.

@jacque
I did try removing all but mousexx handlers however, and this is probably mobGUI, it didn't show the button as being pressed, i.e. didn't change colour.
The variables is where I really found the loop problem to be, I had set the global variable gLoopsToPlay in the stack script, however I didn't name it in the group scripts, so it was just creating a script local variable. Thanks for putting me on to that. Still sounds were not looping though.

@Klaus
After checking and doing the things suggested by @monte and @jacque, it was the wait command that forced the sound to play a second time.

A big HOWEVER though…. the Stop Button no longer works…… I'm still digging into it and using @monte suggestion of the multi-channel sound might work. If you notice something glaringly wrong in the code though please advise…. code now looks like..


Stack Script
==========
global gLoopsToPlay
put 1 into gLoopsToPlay


Option Button Script
=================
global gLoopsToPlay
on touchEnd pId
mobGUIUntouch the long id of me
end touchEnd
on touchRelease pId
mobGUIUntouch the long id of me
end touchRelease
on touchStart pId
mobGUITouch the long id of me
end touchStart
on preOpenControl
if the uMobGUIDefault of me then mobGUITouch the long id of me, "default"
end preOpenControl
on mouseDown
put 5 into gLoopsToPlay
set the uText of group "times" to gLoopsToPlay
end mouseDown
on mouseRelease
if the environment = "development" then touchRelease 1
end mouseRelease
on mouseDown
if the environment = "development" then touchStart 1
end mouseDown

Play Button Script
===============

global gLoopsToPlay
on touchEnd pId
mobGUIUntouch the long id of me
end touchEnd
on touchRelease pId
mobGUIUntouch the long id of me
end touchRelease
on touchStart pId
mobGUITouch the long id of me
end touchStart
on mouseUp
if the environment = "development" then touchEnd 1
local lLoopsLeft
put gLoopsToPlay into lLoopsLeft
repeat gLoopsToPlay
set the uText of group "times" to lLoopsLeft
set the uText of group "NowPlaying" to the uText of me
play specialFolderPath("engine") & "/sound.caf"
wait until the sound = "done"
subtract 1 from lLoopsLeft
end repeat
end mouseUp
on mouseRelease
if the environment = "development" then touchRelease 1
end mouseRelease
on mouseDown
if the environment = "development" then touchStart 1
end mouseDown


Stop Button Script
===============
global gLoopsToPlay
on touchEnd pId
mobGUIUntouch the long id of me
end touchEnd
on touchRelease pId
mobGUIUntouch the long id of me
end touchRelease
on touchStart pId
mobGUITouch the long id of me
end touchStart
on mouseUp
play specialFolderPath("engine") & stop
put 1 into gLoopsToPlay
end mouseUp
on mouseRelease
if the environment = "development" then touchRelease 1
end mouseRelease
on mouseDown
if the environment = "development" then touchStart 1
end mouseDown

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7390
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Simple Sound Loop

Post by jacque » Tue Oct 02, 2012 6:02 pm

Code: Select all

play specialFolderPath("engine") & stop
This will look for a sound file named "stop". I think you just want:

play stop
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

db221
Posts: 4
Joined: Mon Sep 24, 2012 4:14 pm

Re: Simple Sound Loop

Post by db221 » Wed Oct 03, 2012 11:07 am

Hi Jacque

Play Stop and play specialFolderPath("engine") & "/sound.caf"

Both stop the the sound when I remove the wait command, however when I remove the wait command the sound doesn't loop.
its almost as if I need a line similar to - wait until the sound = "done" or stop button is pressed. However I think if I do this it will only stop at the end of a loop iteration and not instantly.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7390
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Simple Sound Loop

Post by jacque » Wed Oct 03, 2012 9:08 pm

Any "play" command will stop the currently playing sound and start the new sound. In this case, the engine tries to play a sound named "stop", which doesn't exist, so you don't hear anything. I.e., sounds will stop with either method, but for different reasons. With "play stop" the engine isn't looking for a file, it just aborts the current sound playback.

There is an iOS message that will tell you when the current sound is finished: soundFinishedOnChannel. Trapping that event will let you start up the next sound, or play the same one again. However, I think you'll need to implement sound channels to use it.

But for this, using sound channels is good because it allows a seamless loop. Play your sound on a channel (mobilePlaySoundOnChannel filename,1,"now") and in the next line of script, queue up the next repeat with (mobilePlaySoundOnChannel filename,1,"next"). That will automatically play the queued sound immediately after the first one finishes. A soundFinishedOnChannel handler can then queue up another repeat by calling mobilePlaySoundOnChannel with the "next" parameter again.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

db221
Posts: 4
Joined: Mon Sep 24, 2012 4:14 pm

Re: Simple Sound Loop

Post by db221 » Fri Nov 02, 2012 8:49 pm

Thanks all so much for your input, finally got time to get this sorted.
To close out, for anyone looking for similar…

I used
put specialFolderPath("engine") & "/<song file>" into tSound
mobilePlaySoundOnChannel tSound, "current", "looping"
wait for gMinsToPlay seconds with messages

Where gMinsToPlay is number of seconds to play the track.

To stop
mobileStopPlayingOnChannel "current"

Now onto my next problem to investigate…..How to let play continue when lock screen comes on.

Thanks again.

Post Reply