I’m writing a childrens book and need to play a different sound clip for each page. The clip will play if I start it with a button but I want it to automatically start as soon as the page is changed. Here’s my code:
elseif (page == 5) then
media.playSound( "Sound-Forest.mp3" )
local stopAfter10Seconds = function()
media.stopSound()
end
timer.performWithDelay( 10000, stopAfter10Seconds )
Can a sound be started within an if statement or does it have to be tied to an event? I’ve spent hours looking over the forums, sample code and docs and haven’t found anything like this.
Thanks in advance. [import]uid: 6397 topic_id: 3641 reply_id: 303641[/import]
take the stopAfter10Seconds function out of your condition and see if that works
eg
[lua]local function stopAfter10Seconds()
media.stopSound()
end
local function doStuff()
…
elseif (page==5) then
media.playSound(“Sound_Forest.mp3”)
timer.performWithDelay(10000,stopAfter10Seconds)
…
end[/lua]
presumably your problem is that stopAfter10Seconds in your example is local (ie private) to your other function and therefore the timer callback can’t see it. [import]uid: 6645 topic_id: 3641 reply_id: 11097[/import]
Thanks for the quick reply jmp909 but unfortunately no luck. I even tried removing everything but the playSound statemen leaving me with the following:
elseif (page == 5) then
media.playSound( "Sound-Forest.mp3" )
“Extended Sounds - For longer sounds (which are generally in MP3 format on iPhoneOS, and often used to play background music), there are several functions that allow you to play, pause, and stop playing a sound. However, you can only have one such long sound file open at a time. These functions all operate on the sound that is opened by the previous call to media.playSound.”
function playSnd(snd)
media.stopSound()
media.playSound( snd )
print(snd)
end
function check4Anim(page)
if (page == 3) then
playSnd("Sound-Train.mp3")
elseif (page == 5) then
playSnd("Sound-Forest.mp3")
end
end
check4Anim(page) is called from my page turn event.
print(snd) results in the correct file name being written to the terminal window.
[import]uid: 6397 topic_id: 3641 reply_id: 11135[/import]
SHEESH…Turns out I DID have a sound loaded already. Once I fixed that everything started working the way it’s supposed too. Now if we could only play more than one sound at a time…:b [import]uid: 6397 topic_id: 3641 reply_id: 11416[/import]