Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Sometimes audio.play doesn't play sounds... just sometimes
Started by nml2727 Dec 28 2019 07:49 PM

8 replies to this topic
audio audio.play audio.loadstream sound not working audio not working sound issues audio issues
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

nml2727

[GLOBAL: userInfoPane.html]
nml2727
  • Enthusiast

  • 96 posts
  • Corona SDK

I'm facing a really weird issue.... Sometimes when my code calls audio.play, no sound is played.  BUT only sometimes... Seems like 80% of the time it works as expected, but every once in a while there is just no sound...

 

I have a function playBackgroundMusic which I call at the start of the game to play the background music... see below: 

 

However, every once in a while no sound is played.  It seems to happen more often upon the first launch of the game, but not consistently.  Maybe 2 out of 10 times there is no sound, and the other 8 times it will work just fine. 

 

In my destroy when I change scenes, I call disposeBackgroundMusic() which is also below. 

 

I also do an audio.stop with a pairs loop to dispose of other sound effects that are unrelated to the background music. 

 

What I don't understand is why it works most of the time, but occasionally I get no sound.  Does anyone see any issues?

local function playBackgroundMusic()

	local randomSong = math.random(1,6)
	if(randomSong == 1) then
		print("playing aurea...")
		backgroundMusic = audio.loadStream("sounds/aurea.mp3")
		audio.play( backgroundMusic, { channel=1, loops=-1 } )
	elseif(randomSong == 2) then
		print("playing carefree...")
		backgroundMusic = audio.loadStream("sounds/carefree.mp3")
		audio.play( backgroundMusic, { channel=1, loops=-1 } )
	elseif(randomSong == 3) then
		print("playing fretless...")
		backgroundMusic = audio.loadStream("sounds/fretless.mp3")
		audio.play( backgroundMusic, { channel=1, loops=-1 } )
	elseif(randomSong == 4) then
		print("playing rainbows...")
		backgroundMusic = audio.loadStream("sounds/rainbows.mp3")
		audio.play( backgroundMusic, { channel=1, loops=-1 } )
	elseif(randomSong == 5) then
		print("playing upbeat...")
		backgroundMusic = audio.loadStream("sounds/upbeat.mp3")
		audio.play( backgroundMusic, { channel=1, loops=-1 } )
	elseif(randomSong == 6) then
		print("playing wallpaper...")
		backgroundMusic = audio.loadStream("sounds/wallpaper.mp3")
		audio.play( backgroundMusic, { channel=1, loops=-1 } )
	end
end
local function disposeBackgroundMusic()
	audio.stop(1)
	audio.dispose(backgroundMusic)
	backgroundMusic = nil
end


[TOPIC: post.html]
#2

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 809 posts
  • Corona SDK

Is it not working in the simulator or on device?

 

Is there any chance that the "playBackgroundMusic" function is getting triggered more than once?  You could try looking for a free audio channel as well.

local availableChannel = audio.findFreeChannel()
 
audio.play(backgroundMusic, {channel = availableChannel, loops=-1})



[TOPIC: post.html]
#3

andrake12

[GLOBAL: userInfoPane.html]
andrake12
  • Observer

  • 12 posts
  • Corona SDK

Can you confirm that you have reserved channel 1?

 

One guess is that another sound might already be playing there

at the time you start this. That would stop the music from playing.



[TOPIC: post.html]
#4

pixec

[GLOBAL: userInfoPane.html]
pixec
  • Contributor

  • 207 posts
  • Corona SDK

perhaps it’s just lag? You could load all audio beforehand and call them when needed

[TOPIC: post.html]
#5

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 809 posts
  • Corona SDK

@pixec - good point

 

@nml2727 - try preloading all of your sounds on main.lua.  If that fixes it you can try preloading them in a module for more control.



[TOPIC: post.html]
#6

nml2727

[GLOBAL: userInfoPane.html]
nml2727
  • Enthusiast

  • 96 posts
  • Corona SDK

It seems you guys were correct that the audio channel was already being used by other sounds.   I had not been reserving channel 1.  I added    audio.reserveChannels(1)   in main.lua and it seems the sound is playing every time since then.

 

Thank you andrake12 for the suggestion!

 

Thanks sporkfin and pixec for chiming in as well!



[TOPIC: post.html]
#7

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 809 posts
  • Corona SDK

Yeah, that's why I use

local availableChannel = audio.findFreeChannel()

to avoid reserved channels



[TOPIC: post.html]
#8

nml2727

[GLOBAL: userInfoPane.html]
nml2727
  • Enthusiast

  • 96 posts
  • Corona SDK

Do you specify channels every time you play a sound effect?  I have a lot of other sounds where I'm simply doing 

audio.play(sounds.blue_button_sound)

Is it fine to just let corona grab an open channel for me?  



[TOPIC: post.html]
#9

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 809 posts
  • Corona SDK

Yes, that should be fine.  Looking back now I see that I added audio.findFreeChannel( ) to my sound module as a way to track which channel each sound is assigned to.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: audio, audio.play, audio.loadstream, sound not working, audio not working, sound issues, audio issues