****************************************
I thing you didn’t get my question or I’m dumb to ask such question.
No I didn’t understand. The question was unclear.
You said you wanted to stop the sound when it was finished. Why? It’s finished. There is nothing to stop.
Anyways, everything you need to learn about sound playing, sound events, etc can be found in the docs, so I gave you:
- link to api docs: https://docs.coronalabs.com/api/
- link to audio.* page: https://docs.coronalabs.com/api/library/audio/index.html
- link to audio.stop(): https://docs.coronalabs.com/api/library/audio/stop.html
Advice: Try to be clear, concise, and precisewhen asking for help.
That is,
- Write a description that describes what you want to achieve in a meaningful way.
- Use complete sentences.
- Check your spelling and punctuation.
- Don’t use abbreviations or acronyms.
- Supply supporting examples, images, etc. if needed.
- Make sure the post is as short as possible without affecting the ability to be understood.
- Make sure the post asks ONE question and doesn’t meander talking about unrelated aspects or details.
Your question was concise, but I couldn’t make heads or tails of what you are trying to achieve.
PS - Often time, it takes considerable effort to answer questions. So, it is my expectation that people asking questions put in just as much effort and time asking the question as I do helping them with an answer.
Explaining bit further. When I stop the audio it stops immediately before the audio has fully finished playing. So I want it to stop after it has completed playing. The codes below.
local function onTouch(event)
local phase = event.phase
if phase == “began” then
audio.play(soundFile, {loops=-1})
elseif phase == “ended” then
audio.stop(soundFile)
end
end
object:addEventListener(“touch”, onTouch)
end
I thing there’s no answer to it. So lets just forget it.
Again, there is nothing to stop if it has finished playing. I am missing the point of this.
Question 1: Why allow users to stop a sound it you’re not going to stop it immediately?
Question 2: Why stop a sound that has finished playing.
Note: The word you are seeking is think, not thing.
Note2: No need to re-quote quotes. Simple response are fine. Also, it make the post hard to consume for future readers.
Be aware, there is a ‘completed’ event for sounds that may be useful for you.
This might help with what you’re trying to do:
https://docs.coronalabs.com/api/library/audio/play.html#oncomplete-optional
Cause I’m looking for a solution for my space shooter game where I want to stop firing the lasers and the audio after the touch event has ended.
OK. Now I get it.
All of this is clearly outlined in the docs. I think you overlooked the fact that audio.stop() stops a sound channel, not a loaded sound handle.
However, it is also possible that you’re new to the idea of sound playing in general. So, let me walk you through it…
-
Get a free channel: (this returns a number)(https://docs.coronalabs.com/api/library/audio/findFreeChannel.html)
-
Play the sound you want to stop later, on that channel.
-
When you stop, pass in the number of the channel.
This should work:
local channel local function onTouch(event) local phase = event.phase if phase == "began" then channel = audio.findFreeChannel() audio.play(soundFile, { loops=-1, channel = channel }) elseif phase == "ended" then audio.stop(channel) end end object:addEventListener("touch", onTouch)
Once you’ve tried that, and assuming it works, let me suggest a much better solution:
function object.touch( self, event ) local phase = event.phase local id = event.id if phase == "began" then display.getCurrentStage():setFocus( self, id ) self.isFocus = true self.channel = audio.findFreeChannel() audio.play(soundFile, { loops=-1, channel = self.channel }) elseif self.isFocus then if phase == "ended" then display.getCurrentStage():setFocus( self, nil ) self.isFocus = false audio.stop(self.channel) end end return false end object:addEventListener("touch")
This is a more advanced listener. It has these benefits:
- attached to button, so ‘self’ is the button.
- you can use self to store values (a sort of scratch pad) like I did with ‘channel’
- When the button is destroyed, the listener is automatically removed.
- If you touch the button, slide your finger off the button, then lift your finger, the ‘stop sound’ code will still execute.
- Try this with the old listener and notice the ‘ended’ phase never executes so the sound continues forever.
- It is compatible with multi-touch and single-touch configurations.
Is there a reason the original post is just a series of astericks?
Any one else wanting to learn from this thread won’t know what it’s about now.
Rob
I told the OP the question was unclear and I think he got frustrated and wiped it. Eventually, we got this resolved (I believe), but I really wish he’d go back and re-edit the original question and make it clear so future readers can benefit.
I thing you didn’t get my question or I’m dumb to ask such question.
No I didn’t understand. The question was unclear.
You said you wanted to stop the sound when it was finished. Why? It’s finished. There is nothing to stop.
Anyways, everything you need to learn about sound playing, sound events, etc can be found in the docs, so I gave you:
- link to api docs: https://docs.coronalabs.com/api/
- link to audio.* page: https://docs.coronalabs.com/api/library/audio/index.html
- link to audio.stop(): https://docs.coronalabs.com/api/library/audio/stop.html
Advice: Try to be clear, concise, and precisewhen asking for help.
That is,
- Write a description that describes what you want to achieve in a meaningful way.
- Use complete sentences.
- Check your spelling and punctuation.
- Don’t use abbreviations or acronyms.
- Supply supporting examples, images, etc. if needed.
- Make sure the post is as short as possible without affecting the ability to be understood.
- Make sure the post asks ONE question and doesn’t meander talking about unrelated aspects or details.
Your question was concise, but I couldn’t make heads or tails of what you are trying to achieve.
PS - Often time, it takes considerable effort to answer questions. So, it is my expectation that people asking questions put in just as much effort and time asking the question as I do helping them with an answer.
Explaining bit further. When I stop the audio it stops immediately before the audio has fully finished playing. So I want it to stop after it has completed playing. The codes below.
local function onTouch(event)
local phase = event.phase
if phase == “began” then
audio.play(soundFile, {loops=-1})
elseif phase == “ended” then
audio.stop(soundFile)
end
end
object:addEventListener(“touch”, onTouch)
end
I thing there’s no answer to it. So lets just forget it.
Again, there is nothing to stop if it has finished playing. I am missing the point of this.
Question 1: Why allow users to stop a sound it you’re not going to stop it immediately?
Question 2: Why stop a sound that has finished playing.
Note: The word you are seeking is think, not thing.
Note2: No need to re-quote quotes. Simple response are fine. Also, it make the post hard to consume for future readers.
Be aware, there is a ‘completed’ event for sounds that may be useful for you.
This might help with what you’re trying to do:
https://docs.coronalabs.com/api/library/audio/play.html#oncomplete-optional