and loop it, but it doesn’t work.
My project is supposed to play sound indefinitely until somebody presses the stop button. What I cannot understand is when I try to print(s), all I get is nil, so I cannot turn on s.looping. How can I get my sound to loop?
@em2 If you read the built in documentation for sound, you can only loop sounds from an asset pack. Here’s an example of looping the sound Pop. I also added code to stop and start the sound. Tap the screen to stop it, tap again to start it.
function setup()
s=sound("Game Sounds One:Pop 1", .5, 1, 0, true) -- true loops the sound
end
function draw()
background(0)
fill(255)
text("tap screen to start or stop the sound",WIDTH/2,HEIGHT/2)
end
function touched(t)
if t.state==BEGAN then
if not pause then
s:stop()
else
s=sound("Game Sounds One:Pop 1", .5, 1, 0, true)
end
pause=not pause
end
end
The documentation says only sounds from an asset can loop. One way you can loop your sound is with a timer. If your sound last 10 seconds, then have a timer start your sound every 11 seconds.
@em2 Here’s an example of looping sounds that you create yourself. Depending on the length of your sound, you’ll have to modify the tween delay time.
function setup()
play()
end
function draw()
background(40, 40, 50)
end
function play()
s=sound( { Waveform = SOUND_NOISE,AttackTime = 1.2,SustainTime = 1} )
tween.delay(6,play)
end
I got it timed properly and my problem has been solved. Thanks a lot. One final thing that I found strange is that sounds generated with a parameterTable do not return anything, so I cannot stop them. I’lll have to implement my own methods of stopping sound.
The only way I found to stop a parameterTable sound is to just let it stop on its own. So when you want it to stop, you have a variable to not call the tween timer. Here’s an example.
function setup()
str="tap screen to start the sound"
end
function draw()
background(40, 40, 50)
fill(255)
text(str,WIDTH/2,HEIGHT/2)
end
function play()
if playSound then
s=sound( { Waveform = SOUND_NOISE,AttackTime = 1.2,SustainTime = 1} )
tween.delay(6,play)
end
end
function touched(t)
if t.state==BEGAN then
playSound=not playSound -- toggle playSound true/false
if playSound then
str="sound is playing\
\
tap to stop"
play()
else
str="sound will stop when sound cycle is complete\
\
tap to start again"
end
end
end
I pretty much did the same thing, but with a very short SustainTime and rapid loop. I also added minor AttackTime and DecayTime to make it play smoother.