Play sound when page is changed

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" )   
  

Still no luck.

[import]uid: 6397 topic_id: 3641 reply_id: 11099[/import]

have you tried

print(page)
print(page==5)

to make sure you get “5” and “true” returned?

is a sound already playing?


“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.”

j [import]uid: 6645 topic_id: 3641 reply_id: 11102[/import]

print(page==5) does = true
There is no sound playing but to make sure I’ve added a stop sound command before the play sound ie:

print(page==5)  
  
 media.stopSound()  
 media.playSound( "Sound-Forest.mp3" )   

[import]uid: 6397 topic_id: 3641 reply_id: 11105[/import]

Have you tried calling a function to play the sound instead?

You can remove stopSound, it’s redundant since it happens automatically when playing a new sound.

[lua]function soundController:loopMusic(thisMusic)
media.playSound( thisMusic, print_function("Played "…thisMusic) )
end[/lua] [import]uid: 11024 topic_id: 3641 reply_id: 11118[/import]

I’ve tried putting it in a separate function like this:

function playSnd(snd)  
 media.stopSound()  
 media.playSound( snd )  
 print(snd)  
end  

The file name gets printed to the terminal but no sound.

If I put the call in a button function ie:

media.playSound( "Sound-Forest.mp3" )   

It plays fine, and if I call the sound function from inside the button event it also works ie:

playSnd("Sound-Forest.mp3")  

It just won’t work without some sort of event other than the “if” event calling it.

[import]uid: 6397 topic_id: 3641 reply_id: 11126[/import]

without seeing more of your code, can’t really comment

j [import]uid: 6645 topic_id: 3641 reply_id: 11128[/import]

Here it is in a nutshell:

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]

hi tarenberg,
if you just use the playSnd(“Sound-Train.mp3”) and the other one, do you hear any sound?

Some MP3’s do not play with the Corona SDK. so it could be that your sounds files might be faulty/incompatible.

NOTE: Do not try to see if it works in any app outside of Corona.

Secondly, are the filenames right, in the sense of the case, that is also important.

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3641 reply_id: 11387[/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]

If you use event sounds, it won’t interfere with the mp3s. [import]uid: 11024 topic_id: 3641 reply_id: 11420[/import]