How to audio.stop( ) all channels but one

Hi I want to stop the audio in all channels {audio.stop ( ) } but one channel , It is posible to leave playing only one channel and stop the rest ?

For example, is this code right:

audio.stop( )—Stop all channels

audio.stop(1)—stop only channel 1

audio.stop(-1) —stop all channels and leave only channel 1 playing ?

if not right how you do it?

Some help please

local function stopAllButOne( keep ) for i = 1, audio.totalChannels do if( i ~= keep ) then audio.stop(i) end end end

Then

stopAllButOne( 5 ) -- stop all known channels but 5

Hi @hec2rin,

I see this is a duplicate post (please resist doing so in the future, so we can more easily track issues and help you solve them).

I responded with my suggestion in the post linked below, which is similar to @roaminggamer’s, but I added in a check for “audio.isChannelActive()” so that the audio engine doesn’t attempt to stop a channel which isn’t being used. If you combine my code with @roaminggamer’s function-based code here, you have a winning solution. :slight_smile:

https://forums.coronalabs.com/topic/59228-how-to-audiostop-all-channels-but-one/

Brent

I’m going to copy Brent’s answer here and get rid of the other thread.

Brent said:

Hi @hec2rin,

If you know that channel 1 is the only channel you want to keep playing, I suggest just looping through the remaining channels and stopping them:

for c = 2,audio.totalChannels do     if ( audio.isChannelActive( c ) ) then         audio.stop( c )     end end

Hope this helps,

Brent


Please post any follow ups on this thread.

Rob

Very good, Thanks for your help and quick responses! works great!

Hi, my app is base on 2 or more objets everyone has a mp3 

local nota1 = audio.loadSound(“M1.mp3”)

local nota2 = audio.loadSound(“M2.mp3”)

–when you began or moved event.phase

local object = display.newImage ( “bStop.png” )

object.x = 200

object.y = 200

object.id = “ball object”

local function stopAllButOne( keep )

   for i = 1, audio.totalChannels do

      if( i ~= keep ) then 

         audio.stop(i)

      end

   end

end

function object:touch( event )

if event.phase == “began” or event.phase == “moved” then

        if(event.target == object) then

        stopAllButOne( 1 )

        audio.play(nota1, {channel=1})

    end

elseif event.phase == “ended” or event.phase == “cancelled” then

audio.stop(1)

   end

end

object:addEventListener(“touch”, object)

Runtime:addEventListener(“touch”, object)

–and the same for the second object

Everything works fine when i move my finger and touch the objects turns on and off the audio as programed it but,

I need a multi touch activated it and when i do that (system.activate…)

and i touch both objects only one sound…What else do i need to do? any help?

Hi @hec2rin,

Well in that case, it seems that you actually want two audio channels playing, correct? So you can’t stop all channels except the first…

Hi Brent,

Yes you are right 2 or more at the same time its all depends how many objects we touch at the same time at first I thought that will work the same on multitouch, but now make sense to me, absolutely my app is going to have like 20 objects doing the same-thing,

any idea? Thanks for replay 

Hi @hec2rin,

Out of curiosity, why is it necessary to stop all other audio channels when you start playing? Normally, I just let the audio engine pick a free channel and play on that channel. Please explain why you need to stop audio before playing new audio. What kind of app is this? What does it basically do?

Of course, if you let the audio engine auto-manage itself, and your app allows more that 32 consecutive sounds, then you’ll have a problem because that’s the limit on available consecutive channels.

Brent

Thanks for everything. Just would like to know why I can not use Multitouch with this code.

Everything works fine, just the way I need it. But when I use Multitouch, it gives me problems

local composer = require( "composer" ) local scene = composer.newScene() system.activate("multitouch") local nota1 = audio.loadSound("Do4.mp3") local nota2 = audio.loadSound("Re4.mp3") local banderita = false -- ------------------------------------------------------------------------------ -- "scene:create()" function scene:create( event ) local sceneGroup = self.view &nbsp; tantanX = localToContent &nbsp; local object = display.newImage ( "bStop.png" ) object.x = 200 object.y = 200 object.id = "ball object" &nbsp; local function stopAllButOne( keep ) &nbsp; &nbsp;for i = 1, audio.totalChannels do &nbsp; &nbsp; &nbsp; if( i ~= keep ) then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;audio.stop(i) &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp;end end &nbsp; function object:touch( event ) if event.phase == "began" or event.phase == "moved" then &nbsp; &nbsp; &nbsp; &nbsp; if(event.target == object) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; audio.play(nota1, {channel=1}) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif(event.phase == "moved") then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(event.x \< object.contentBounds.xMin) or &nbsp; &nbsp; &nbsp; &nbsp; (event.x \> object.contentBounds.xMax) or &nbsp; &nbsp; &nbsp; &nbsp; (event.y \< object.contentBounds.yMin) or &nbsp; &nbsp; &nbsp; &nbsp; (event.y \> object.contentBounds.yMax) then &nbsp; &nbsp; &nbsp; &nbsp; audio.stop(1) &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; elseif event.phase == "ended" or event.phase == "cancelled" then &nbsp; &nbsp; &nbsp; &nbsp; audio.stop(1) &nbsp; &nbsp; end end object:addEventListener("touch", object) Runtime:addEventListener("touch", object) &nbsp; local object2 = display.newImage ( "bStop.png" ) object2.x = 450 object2.y = 200 object2.id = "ball object2" &nbsp; function object2:touch( event2 ) if event2.phase == "began" or event2.phase == "moved" then &nbsp; &nbsp; &nbsp; &nbsp; if(event2.target == object2) then &nbsp; &nbsp; &nbsp; &nbsp; audio.play(nota2, {channel=2}) &nbsp; &nbsp; &nbsp; &nbsp; elseif(event2.phase == "moved") then &nbsp; &nbsp; &nbsp; &nbsp; if(event2.x \< object2.contentBounds.xMin) or &nbsp; &nbsp; &nbsp; &nbsp; (event2.x \> object2.contentBounds.xMax) or &nbsp; &nbsp; &nbsp; &nbsp; (event2.y \< object2.contentBounds.yMin) or &nbsp; &nbsp; &nbsp; &nbsp; (event2.y \> object2.contentBounds.yMax) then &nbsp; &nbsp; &nbsp; &nbsp; audio.stop(2) &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; end &nbsp; &nbsp; elseif(event2.phase == "ended") then &nbsp; &nbsp; &nbsp; &nbsp; audio.stop() &nbsp; &nbsp; end end &nbsp; object2:addEventListener( "touch", object2 ) Runtime:addEventListener("touch", object2) &nbsp;&nbsp; end -- ------------------------------------------------------------------------------ -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase &nbsp; &nbsp; if ( phase == "will" ) then&nbsp; &nbsp; &nbsp; elseif ( phase == "did" ) then &nbsp; &nbsp; end end -- ------------------------------------------------------------------------------ -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase &nbsp; &nbsp; if ( phase == "will" ) then &nbsp; &nbsp; elseif ( phase == "did" ) then &nbsp; &nbsp; end end -- ------------------------------------------------------------------------------ -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) -- ------------------------------------------------------------------------------ return scene

Hi @hec2rin,

I would like to help you more, but multitouch is a very specific process that must be crafted according to your app. Corona provides you with the ability to get a unique “touch ID” for each touch on the screen, along with the ability to set the “focus” of each touch on a particular object. From there, you need to figure out how to handle it in your project, and what should happen on each phase of each touch, along with what happens when a touch loses focus on the object it started on. We (staff) can’t really know what your project is supposed to do and how it’s supposed to behave with multitouch.

I can suggest that you look here for details on setting touch focus:

https://docs.coronalabs.com/api/type/StageObject/setFocus.html

Best regards,

Brent

local function stopAllButOne( keep ) for i = 1, audio.totalChannels do if( i ~= keep ) then audio.stop(i) end end end

Then

stopAllButOne( 5 ) -- stop all known channels but 5

Hi @hec2rin,

I see this is a duplicate post (please resist doing so in the future, so we can more easily track issues and help you solve them).

I responded with my suggestion in the post linked below, which is similar to @roaminggamer’s, but I added in a check for “audio.isChannelActive()” so that the audio engine doesn’t attempt to stop a channel which isn’t being used. If you combine my code with @roaminggamer’s function-based code here, you have a winning solution. :slight_smile:

https://forums.coronalabs.com/topic/59228-how-to-audiostop-all-channels-but-one/

Brent

I’m going to copy Brent’s answer here and get rid of the other thread.

Brent said:

Hi @hec2rin,

If you know that channel 1 is the only channel you want to keep playing, I suggest just looping through the remaining channels and stopping them:

for c = 2,audio.totalChannels do &nbsp; &nbsp; if ( audio.isChannelActive( c ) ) then &nbsp; &nbsp; &nbsp; &nbsp; audio.stop( c ) &nbsp; &nbsp; end end

Hope this helps,

Brent


Please post any follow ups on this thread.

Rob

Very good, Thanks for your help and quick responses! works great!

Hi, my app is base on 2 or more objets everyone has a mp3 

local nota1 = audio.loadSound(“M1.mp3”)

local nota2 = audio.loadSound(“M2.mp3”)

–when you began or moved event.phase

local object = display.newImage ( “bStop.png” )

object.x = 200

object.y = 200

object.id = “ball object”

local function stopAllButOne( keep )

   for i = 1, audio.totalChannels do

      if( i ~= keep ) then 

         audio.stop(i)

      end

   end

end

function object:touch( event )

if event.phase == “began” or event.phase == “moved” then

        if(event.target == object) then

        stopAllButOne( 1 )

        audio.play(nota1, {channel=1})

    end

elseif event.phase == “ended” or event.phase == “cancelled” then

audio.stop(1)

   end

end

object:addEventListener(“touch”, object)

Runtime:addEventListener(“touch”, object)

–and the same for the second object

Everything works fine when i move my finger and touch the objects turns on and off the audio as programed it but,

I need a multi touch activated it and when i do that (system.activate…)

and i touch both objects only one sound…What else do i need to do? any help?

Hi @hec2rin,

Well in that case, it seems that you actually want two audio channels playing, correct? So you can’t stop all channels except the first…

Hi Brent,

Yes you are right 2 or more at the same time its all depends how many objects we touch at the same time at first I thought that will work the same on multitouch, but now make sense to me, absolutely my app is going to have like 20 objects doing the same-thing,

any idea? Thanks for replay 

Hi @hec2rin,

Out of curiosity, why is it necessary to stop all other audio channels when you start playing? Normally, I just let the audio engine pick a free channel and play on that channel. Please explain why you need to stop audio before playing new audio. What kind of app is this? What does it basically do?

Of course, if you let the audio engine auto-manage itself, and your app allows more that 32 consecutive sounds, then you’ll have a problem because that’s the limit on available consecutive channels.

Brent

Thanks for everything. Just would like to know why I can not use Multitouch with this code.

Everything works fine, just the way I need it. But when I use Multitouch, it gives me problems

local composer = require( "composer" ) local scene = composer.newScene() system.activate("multitouch") local nota1 = audio.loadSound("Do4.mp3") local nota2 = audio.loadSound("Re4.mp3") local banderita = false -- ------------------------------------------------------------------------------ -- "scene:create()" function scene:create( event ) local sceneGroup = self.view &nbsp; tantanX = localToContent &nbsp; local object = display.newImage ( "bStop.png" ) object.x = 200 object.y = 200 object.id = "ball object" &nbsp; local function stopAllButOne( keep ) &nbsp; &nbsp;for i = 1, audio.totalChannels do &nbsp; &nbsp; &nbsp; if( i ~= keep ) then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;audio.stop(i) &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp;end end &nbsp; function object:touch( event ) if event.phase == "began" or event.phase == "moved" then &nbsp; &nbsp; &nbsp; &nbsp; if(event.target == object) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; audio.play(nota1, {channel=1}) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif(event.phase == "moved") then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(event.x \< object.contentBounds.xMin) or &nbsp; &nbsp; &nbsp; &nbsp; (event.x \> object.contentBounds.xMax) or &nbsp; &nbsp; &nbsp; &nbsp; (event.y \< object.contentBounds.yMin) or &nbsp; &nbsp; &nbsp; &nbsp; (event.y \> object.contentBounds.yMax) then &nbsp; &nbsp; &nbsp; &nbsp; audio.stop(1) &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; elseif event.phase == "ended" or event.phase == "cancelled" then &nbsp; &nbsp; &nbsp; &nbsp; audio.stop(1) &nbsp; &nbsp; end end object:addEventListener("touch", object) Runtime:addEventListener("touch", object) &nbsp; local object2 = display.newImage ( "bStop.png" ) object2.x = 450 object2.y = 200 object2.id = "ball object2" &nbsp; function object2:touch( event2 ) if event2.phase == "began" or event2.phase == "moved" then &nbsp; &nbsp; &nbsp; &nbsp; if(event2.target == object2) then &nbsp; &nbsp; &nbsp; &nbsp; audio.play(nota2, {channel=2}) &nbsp; &nbsp; &nbsp; &nbsp; elseif(event2.phase == "moved") then &nbsp; &nbsp; &nbsp; &nbsp; if(event2.x \< object2.contentBounds.xMin) or &nbsp; &nbsp; &nbsp; &nbsp; (event2.x \> object2.contentBounds.xMax) or &nbsp; &nbsp; &nbsp; &nbsp; (event2.y \< object2.contentBounds.yMin) or &nbsp; &nbsp; &nbsp; &nbsp; (event2.y \> object2.contentBounds.yMax) then &nbsp; &nbsp; &nbsp; &nbsp; audio.stop(2) &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; end &nbsp; &nbsp; elseif(event2.phase == "ended") then &nbsp; &nbsp; &nbsp; &nbsp; audio.stop() &nbsp; &nbsp; end end &nbsp; object2:addEventListener( "touch", object2 ) Runtime:addEventListener("touch", object2) &nbsp;&nbsp; end -- ------------------------------------------------------------------------------ -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase &nbsp; &nbsp; if ( phase == "will" ) then&nbsp; &nbsp; &nbsp; elseif ( phase == "did" ) then &nbsp; &nbsp; end end -- ------------------------------------------------------------------------------ -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase &nbsp; &nbsp; if ( phase == "will" ) then &nbsp; &nbsp; elseif ( phase == "did" ) then &nbsp; &nbsp; end end -- ------------------------------------------------------------------------------ -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) -- ------------------------------------------------------------------------------ return scene