Creating audio channel to have full control about every single audio - HELP

Hey there,

i want to more professional with audio.
Currently I just stop the entire audio with muting them all. 

SoundTable = {    \_BACKGROUNDMUSIC = audio.loadStream("audio/bk.mp3"),    \_LEVELPLAY = audio.loadStream("audio/Accept.mp3"),     \_WINDE = audio.loadSound( "audio/bip.mp3" ),     \_ENEMYHIT = audio.loadSound( "audio/enemy-hit.au" ), } audio.play(soundTable["\_BACKGROUNDMUSIC"], {loops=-1, fadein=2500}) audio.setVolume( 0 )-- DO NOT WANNA SOLF it in that way... not enough any more... audio.stop(soundTable["\_BACKGROUNDMUSIC"], {loops=-1, fadein=2500})-- does not work

when I enter the scene I receive this message in corona terminal 

 scene/game.lua:130: audio.stop() called with unexpected parameter type

I want to be able to stop audio separately from background and all the other sounds.

Can Anyone help me, Please? 

I did not understand how channels are working,… is there any template? I thought like above I have a table and can access to every single sound but it is not working…

Best regards and thanks in advance 

 

  1. Better go read the docs again.   You’ve got a number of errors in your code. 
     

  2. You are stopping the wrong thing.  You need to stop the channel.
     

  3. SOLF?  Please try to use proper English if you can.  It makes understanding and responding to the post easier for stick-in-the-mud types like myself.  If English is not your first language, then please ignore this comment.
     
    4. I don’t think *.au  is a universally supported format.  You should use Audacity or some other tool to convert that sound file.
     
    Your code modified like this should work:
     
    a. Create this module in a file named soundMgr.lua (may contain typos):

    local soundMgr = {} – Initialize Sound Table – Allocate one channel per sound (not best way but works for limited number of sounds) local soundTable = {} – local lastChannel = 5 – lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.backgroundMusic = { handle = audio.loadStream(“audio/bk.mp3”), channel = lastChannel } – lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.levelMusic = { handle = audio.loadStream(“audio/Accept.mp3”), channel = lastChannel } – lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.windEffect = { handle = audio.loadSound( “audio/bip.mp3” ), channel = lastChannel } – lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.enemyHit = { handle = loadSound( “audio/enemy-hit.au” ), channel = lastChannel } – -- sound.play( name [, params] ) - Play ‘name’ sound with optional params. – function soundMgr.play( name, params ) params = params or {} – if( not soundTable[name] ) then print("Warning! soundMgr.play() - Sound “, name, " not found in sound table.” ) return end – if( audio.isChannelActive( soundTable[name].channel ) ) then return end – params.channel = soundTable[name].channel – audio.play( soundTable[name].handle, params ) end – -- sound.stop( name ) - Play ‘name’ sound if playing. – function soundMgr.stop( name ) if( not soundTable[name] ) then print("Warning! soundMgr.stop() - Sound “, name, " not found in sound table.” ) return end – if( not audio.isChannelActive( soundTable[name].channel ) or not audio.isChannelPlaying( soundTable[name].channel ) ) then return end – audio.seek( 0, { channel = soundTable[name].channel } ) – **UPDATE** audio.stop( soundTable[name].channel ) – **UPDATE** end return soundMgr

b. Require it once in main.lua

require "soundMgr"

c. Require it where you need to use it:

local soundMgr = require "soundMgr" soundMgr.play( "backgroundMusic", { loops = -1, fadein = 1000 } ) -- stop it in 5 seconds to demo concept timer.peformWithDelay( 5000 function() soundMgr.stop( "backgroundMusic" ) end ) -- Play hit sound in 10 seconds and 15 seconds as demo: timer.peformWithDelay( 10000 function() soundMgr.play( "enemyHit" ) end ) timer.peformWithDelay( 15000 function() soundMgr.play( "enemyHit" ) end )

Thank you very much for the quick response! I got the Background music working to play. However, If the function calls to stop Backgroundmusic an error appears in the terminal. Unfortunately, It does not stop. 

following line 
ERROR: soundmgr.lua:56: audio.stop() called with unexpected parameter type

Part 1 

local soundmgr = {} -- Initialize Sound Table -- Allocate one channel per sound (not best way but works for limited number of sounds) local soundTable = {} -- local lastChannel = 4 -- lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.backgroundMusic = { handle = audio.loadStream("audio/080415pianobgm3popver\_0\_02.mp3"), channel = lastChannel } -- lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.levelMusic = { handle = audio.loadStream("audio/Accept.mp3"), channel = lastChannel } -- lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.windEffect = { handle = audio.loadSound( "audio/short\_wind\_sound.mp3" ), channel = lastChannel } -- lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.enemyHit = { handle = audio.loadSound( "audio/enemy-hit.au" ), channel = lastChannel } -- -- sound.play( name [, params] ) - Play 'name' sound with optional params. -- function soundmgr.play( name, params ) params = params or {} -- if( not soundTable[name] ) then print("Warning! soundMgr.play() - Sound ", name, " not found in sound table." ) return end -- if( audio.isChannelActive( soundTable[name].channel ) ) then return end -- params.channel = soundTable[name].channel -- audio.play( soundTable[name].handle, params ) end -- -- sound.stop( name ) - Play 'name' sound if playing. -- function soundmgr.stop( name ) if( not soundTable[name] ) then print("Warning! soundMgr.stop() - Sound ", name, " not found in sound table." ) return end -- if( not audio.isChannelActive( soundTable[name].channel ) or not audio.isChannelPlaying( soundTable[name].channel ) ) then return end -- audio.seek( 0, { channel = soundTable[name].handle } ) audio.stop( soundTable[name].handle ) end return soundmgr

Part 2

 

local soundmgr = require "soundmgr" soundmgr.play( "backgroundMusic", { loops = 1, fadein = 1000 } ) -- stop it in 5 seconds to demo concept local myClosure = function() soundmgr.stop( "backgroundMusic" ) end timer.performWithDelay( 500, myClosure, 1 )

The music is interrupted and starts from the beginning but does not stop totally… Thanks in advance! 

ERROR: soundmgr.lua:56: audio.stop() called with unexpected parameter type

My corona build is the latest one. (2017.3183)

I agree with speaking properly english. Even if im not native speaker feel free to correct me. 

You can debug the code by putting print statements before the error line to see what value is being passed into stop()

Either I made a typo or if you changed the code you may have. Also, be sure to read the docs for each function.

 

I see the issue (typo on my part)

I passed the handle when I meant to type channel.  I have updated my ORIGINAL CODE POST.

Changes marked with   **UPDATE**.

If there are any more issues, go line by line and compare the types of values I’m passing in versus the docs for that function.

I don’t promise I didn’t make more typos.

I can’t help any more for now as I’m over-whelmed with high priority items.

Thank you so much! 

It’s working! It s amazing!

  1. Better go read the docs again.   You’ve got a number of errors in your code. 
     

  2. You are stopping the wrong thing.  You need to stop the channel.
     

  3. SOLF?  Please try to use proper English if you can.  It makes understanding and responding to the post easier for stick-in-the-mud types like myself.  If English is not your first language, then please ignore this comment.
     
    4. I don’t think *.au  is a universally supported format.  You should use Audacity or some other tool to convert that sound file.
     
    Your code modified like this should work:
     
    a. Create this module in a file named soundMgr.lua (may contain typos):

    local soundMgr = {} – Initialize Sound Table – Allocate one channel per sound (not best way but works for limited number of sounds) local soundTable = {} – local lastChannel = 5 – lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.backgroundMusic = { handle = audio.loadStream(“audio/bk.mp3”), channel = lastChannel } – lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.levelMusic = { handle = audio.loadStream(“audio/Accept.mp3”), channel = lastChannel } – lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.windEffect = { handle = audio.loadSound( “audio/bip.mp3” ), channel = lastChannel } – lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.enemyHit = { handle = loadSound( “audio/enemy-hit.au” ), channel = lastChannel } – -- sound.play( name [, params] ) - Play ‘name’ sound with optional params. – function soundMgr.play( name, params ) params = params or {} – if( not soundTable[name] ) then print("Warning! soundMgr.play() - Sound “, name, " not found in sound table.” ) return end – if( audio.isChannelActive( soundTable[name].channel ) ) then return end – params.channel = soundTable[name].channel – audio.play( soundTable[name].handle, params ) end – -- sound.stop( name ) - Play ‘name’ sound if playing. – function soundMgr.stop( name ) if( not soundTable[name] ) then print("Warning! soundMgr.stop() - Sound “, name, " not found in sound table.” ) return end – if( not audio.isChannelActive( soundTable[name].channel ) or not audio.isChannelPlaying( soundTable[name].channel ) ) then return end – audio.seek( 0, { channel = soundTable[name].channel } ) – **UPDATE** audio.stop( soundTable[name].channel ) – **UPDATE** end return soundMgr

b. Require it once in main.lua

require "soundMgr"

c. Require it where you need to use it:

local soundMgr = require "soundMgr" soundMgr.play( "backgroundMusic", { loops = -1, fadein = 1000 } ) -- stop it in 5 seconds to demo concept timer.peformWithDelay( 5000 function() soundMgr.stop( "backgroundMusic" ) end ) -- Play hit sound in 10 seconds and 15 seconds as demo: timer.peformWithDelay( 10000 function() soundMgr.play( "enemyHit" ) end ) timer.peformWithDelay( 15000 function() soundMgr.play( "enemyHit" ) end )

Thank you very much for the quick response! I got the Background music working to play. However, If the function calls to stop Backgroundmusic an error appears in the terminal. Unfortunately, It does not stop. 

following line 
ERROR: soundmgr.lua:56: audio.stop() called with unexpected parameter type

Part 1 

local soundmgr = {} -- Initialize Sound Table -- Allocate one channel per sound (not best way but works for limited number of sounds) local soundTable = {} -- local lastChannel = 4 -- lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.backgroundMusic = { handle = audio.loadStream("audio/080415pianobgm3popver\_0\_02.mp3"), channel = lastChannel } -- lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.levelMusic = { handle = audio.loadStream("audio/Accept.mp3"), channel = lastChannel } -- lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.windEffect = { handle = audio.loadSound( "audio/short\_wind\_sound.mp3" ), channel = lastChannel } -- lastChannel = audio.findFreeChannel(lastChannel+1) soundTable.enemyHit = { handle = audio.loadSound( "audio/enemy-hit.au" ), channel = lastChannel } -- -- sound.play( name [, params] ) - Play 'name' sound with optional params. -- function soundmgr.play( name, params ) params = params or {} -- if( not soundTable[name] ) then print("Warning! soundMgr.play() - Sound ", name, " not found in sound table." ) return end -- if( audio.isChannelActive( soundTable[name].channel ) ) then return end -- params.channel = soundTable[name].channel -- audio.play( soundTable[name].handle, params ) end -- -- sound.stop( name ) - Play 'name' sound if playing. -- function soundmgr.stop( name ) if( not soundTable[name] ) then print("Warning! soundMgr.stop() - Sound ", name, " not found in sound table." ) return end -- if( not audio.isChannelActive( soundTable[name].channel ) or not audio.isChannelPlaying( soundTable[name].channel ) ) then return end -- audio.seek( 0, { channel = soundTable[name].handle } ) audio.stop( soundTable[name].handle ) end return soundmgr

Part 2

 

local soundmgr = require "soundmgr" soundmgr.play( "backgroundMusic", { loops = 1, fadein = 1000 } ) -- stop it in 5 seconds to demo concept local myClosure = function() soundmgr.stop( "backgroundMusic" ) end timer.performWithDelay( 500, myClosure, 1 )

The music is interrupted and starts from the beginning but does not stop totally… Thanks in advance! 

ERROR: soundmgr.lua:56: audio.stop() called with unexpected parameter type

My corona build is the latest one. (2017.3183)

I agree with speaking properly english. Even if im not native speaker feel free to correct me. 

You can debug the code by putting print statements before the error line to see what value is being passed into stop()

Either I made a typo or if you changed the code you may have. Also, be sure to read the docs for each function.

 

I see the issue (typo on my part)

I passed the handle when I meant to type channel.  I have updated my ORIGINAL CODE POST.

Changes marked with   **UPDATE**.

If there are any more issues, go line by line and compare the types of values I’m passing in versus the docs for that function.

I don’t promise I didn’t make more typos.

I can’t help any more for now as I’m over-whelmed with high priority items.

Thank you so much! 

It’s working! It s amazing!