Audio not stopping

Ok, I’m following a video tutorial and I have an issue. The idea is that when you click the button 5 times the soundtrack should stop but it doesn’t. I checked my code with the one in the video so  many times, I can’t find a difference. This is how it looks: 

local \_W = display.viewableContentWidth local \_H = display.viewableContentHeight local c = 0 display.setDefault("background", 1,1,1) local pop\_sound = audio.loadSound("pop.mp3") local soundtrack = audio.loadStream("ring.mp3") local btn = display.newImageRect("btn.png", 250, 125) btn.x = \_W/2 btn.y = \_H/2 local function btntouch(e) if(e.phase == "ended") then c = c + 1 audio.play(pop\_sound) if(c == 5) then audio.stop(soundtrack) end end end btn:addEventListener("touch", btntouch) audio.play(soundtrack, {loops = -1})

if needed I will post a link to the video. 
Thanks. 

You should post a link to the video since you mentioned it. :slight_smile:

Also, have your read the audio.* API docs?

I ask, because that code is wrong.  Step 1 should be to check each function versus the docs to see if there is a change or error. 

I understand wanting to simply follow the video tut, but if it is old, something may have changed.  Alternately, the person who made it may have goofed.  

In any case, you need to stop the channel or all sounds, not the handle to the audio file.  See linked docs in code.

local \_W = display.viewableContentWidth local \_H = display.viewableContentHeight local c = 0 display.setDefault("background", 1,1,1) local pop\_sound = audio.loadSound("pop.mp3") local soundtrack = audio.loadStream("ring.mp3") local btn = display.newImageRect("btn.png", 250, 125) local soundTrackChannel btn.x = \_W/2 btn.y = \_H/2 local function btntouch(e) if(e.phase == "ended") then c = c + 1 audio.play(pop\_sound) if(c == 5) then -- https://docs.coronalabs.com/api/library/audio/stop.html audio.stop( soundTrackChannel ) end end end btn:addEventListener("touch", btntouch) --https://docs.coronalabs.com/api/library/audio/findFreeChannel.html soundTrackChannel = audio.findFreeChannel( 2 ) -- https://docs.coronalabs.com/api/library/audio/play.html audio.play(soundtrack, { channel = soundTrackChannel, loops = -1})

Thank you very much for the response. I did not look into the API docs but I will certainly do so now. Thank you for the links. 

Video here.

I know it is old, but I really liked how the guy explains stuff. 

You should post a link to the video since you mentioned it. :slight_smile:

Also, have your read the audio.* API docs?

I ask, because that code is wrong.  Step 1 should be to check each function versus the docs to see if there is a change or error. 

I understand wanting to simply follow the video tut, but if it is old, something may have changed.  Alternately, the person who made it may have goofed.  

In any case, you need to stop the channel or all sounds, not the handle to the audio file.  See linked docs in code.

local \_W = display.viewableContentWidth local \_H = display.viewableContentHeight local c = 0 display.setDefault("background", 1,1,1) local pop\_sound = audio.loadSound("pop.mp3") local soundtrack = audio.loadStream("ring.mp3") local btn = display.newImageRect("btn.png", 250, 125) local soundTrackChannel btn.x = \_W/2 btn.y = \_H/2 local function btntouch(e) if(e.phase == "ended") then c = c + 1 audio.play(pop\_sound) if(c == 5) then -- https://docs.coronalabs.com/api/library/audio/stop.html audio.stop( soundTrackChannel ) end end end btn:addEventListener("touch", btntouch) --https://docs.coronalabs.com/api/library/audio/findFreeChannel.html soundTrackChannel = audio.findFreeChannel( 2 ) -- https://docs.coronalabs.com/api/library/audio/play.html audio.play(soundtrack, { channel = soundTrackChannel, loops = -1})

Thank you very much for the response. I did not look into the API docs but I will certainly do so now. Thank you for the links. 

Video here.

I know it is old, but I really liked how the guy explains stuff.