Hi. My template game gives audio error.
“Audio Error: Requested Channel (0) is in use”
Music is looping. When i finish the game and return the menu it gives this error.
local backgroundMusic = audio.loadStream( “musicz/background_music.mp3” )
local function startButtonListener( event )
config[‘character’] = 1
storyboard.gotoScene(“game”)
return true --prevents touch propagation to underlying objects
end
local function creditsButtonListener( event )
storyboard.gotoScene(“mycredits”)
return true --prevents touch propagation to underlying objects
end
local function highScoreButtonListener( event )
storyboard.gotoScene(“highscores”)
return true --prevents touch propagation to underlying objects
end
local function pressAudio( event )
if config[‘sound’] then
soundOn:setFillColor(1,1,1,0.01)
soundOff:setFillColor(1,1,1,1)
if audio.isChannelPlaying(1) then – 1 is the audio channel
audio.stop(1)
end
config[‘sound’] = false
preference.save{sound = false}
else
soundOn:setFillColor(1,1,1,1)
soundOff:setFillColor(1,1,1,0.01)
if audio.isChannelPlaying(1) then – 1 is the audio channel
audio.stop(1)
end
audio.play( backgroundMusic, { loops=-1, channel=1 } )
config[‘sound’] = true
preference.save{sound = true}
end
return true
end
function scene:createScene( event )
local group = self.view
w = display.screenOriginX+display.actualContentWidth
h = display.screenOriginY+display.actualContentHeight
backgroundImg = display.newImage( “images/background.png” )
backgroundImgL = display.newImage( “images/background.png” )
backgroundImgR = display.newImage( “images/background.png” )
backgroundImg.width = 640
backgroundImg.height = 1136
backgroundImgL.width = 640
backgroundImgL.height = 1136
backgroundImgR.width = 640
backgroundImgR.height = 1136
– set starting backgrounds positions
backgroundImg.x = backgroundImg.width/2
backgroundImg.y = backgroundImg.height/2
backgroundImgL.x = backgroundImg.x + backgroundImgL.width
backgroundImgL.y = backgroundImg.height/2
backgroundImgR.x = backgroundImg.x - backgroundImgR.width
backgroundImgR.y = backgroundImg.height/2
tmpStartPlat = display.newImage(“images/platform.png”)
tmpStartPlatL = display.newImage(“images/platform.png”)
tmpStartPlatR = display.newImage(“images/platform.png”)
tmpStartPlat.width = 640
tmpStartPlat.height = 300
tmpStartPlatL.width = 640
tmpStartPlatL.height = 300
tmpStartPlatR.width = 640
tmpStartPlatR.height = 300
tmpStartPlat.x = tmpStartPlat.width/2
tmpStartPlat.y = h-tmpStartPlat.height/2
tmpStartPlatL.x = tmpStartPlat.x + tmpStartPlatL.width
tmpStartPlatL.y = h-tmpStartPlat.height/2
tmpStartPlatR.x = tmpStartPlat.x - tmpStartPlatR.width
tmpStartPlatR.y = h-tmpStartPlat.height/2
logoImg = display.newImage( “images/homelogo.png” )
logoImg.x = display.contentWidth/2
logoImg.y = -240
display.setDefault( ‘magTextureFilter’, ‘nearest’ )
startImg = display.newImage( “images/btn1_start.png” )
startImg.x = display.contentWidth/2
startImg.y = display.contentHeight/2
creditsImg = display.newImage(“images/creditz.png”)
creditsImg.x = display.contentWidth/2
creditsImg.y = startImg.y + 100
highScoreImg = display.newImage( “images/high-score.png” )
highScoreImg.x = display.contentWidth/2
highScoreImg.y = creditsImg.y + 100
soundOff = display.newImageRect (“images/audio_off.png”,64,64 )
soundOff.x = display.contentWidth - soundOff.width
soundOff.y = display.screenOriginY + soundOff.height + 50
soundOn = display.newImageRect (“images/audio_on.png”,64,64 )
soundOn.x = display.contentWidth - soundOn.width
soundOn.y = display.screenOriginY + soundOff.height + 50
if config[‘sound’] then
soundOn:setFillColor(1,1,1,1)
soundOff:setFillColor(1,1,1,0.01)
audio.play( backgroundMusic, { loops=-1, channel=1 } )
else
soundOn:setFillColor(1,1,1,0.01)
soundOff:setFillColor(1,1,1,1)
if audio.isChannelPlaying(1) then – 1 is the audio channel
audio.stop(1)
end
end
group:insert(backgroundImg)
group:insert(backgroundImgL)
group:insert(backgroundImgR)
group:insert(tmpStartPlat)
group:insert(tmpStartPlatL)
group:insert(tmpStartPlatR)
group:insert(logoImg)
group:insert(startImg)
group:insert(creditsImg)
group:insert(highScoreImg)
group:insert(soundOff)
group:insert(soundOn)
transition.moveTo( logoImg, { y=260, time=1500, transition=easing.outExpo } )
creditsImg:addEventListener( “tap”, creditsButtonListener )
startImg:addEventListener( “tap”, startButtonListener )
highScoreImg:addEventListener( “tap”, highScoreButtonListener )
soundOn:addEventListener( “tap”, pressAudio )
end