Good evening all. I am about 90% done with a small project of mine and I am currently attempting to put some sound effects into it. I followed the advice given in the API: I loaded my short sound effects in a table. That code is in a Lua file (sounds.lua) that I require in another file (game.lua) where the core game code is. I say that because I am not sure at what point those sounds are loaded when the game is running. Either way, when I call audio.play(), the sounds sometimes don’t play at all or with a slight delay. Can anyone provide some insight into what my be wrong?
You will need to post your sounds.lua file as a start.
local audio = require “audio”
local sound_control = {}
sound_control.sound_table = {
correct = audio.loadSound(“sounds/hit.mp3”)
}
sound_control.play_effect = function(effect)
if sound_control.sound_table[effect] ~= nil then
print(“playing now…”)
audio.play(sound_control.sound_table[effect])
end
end
return sound_control
Well you don’t need to require “audio”. Do you have an “audio.lua”? if so you should not overwrite the build in audio library.
You can also print out the value of effect and make sure it’s what you think your doing. How are you calling the function?
In main.lua, I create a global variable for the sounds.lua file so that I can call that function from anywhere. I assume that when I create that variable, that is when the sounds are loaded. To call the function:
sound_control.play_effect(“correct”)
When I call it, “playing now…” prints out every time, but it only plays the sound sometimes.
You might want to get a channel. See: http://docs.coronalabs.com/api/library/audio/findFreeChannel.html
and try playing the sound on that specific channel.
Rob
That worked. Thank you.
You will need to post your sounds.lua file as a start.
local audio = require “audio”
local sound_control = {}
sound_control.sound_table = {
correct = audio.loadSound(“sounds/hit.mp3”)
}
sound_control.play_effect = function(effect)
if sound_control.sound_table[effect] ~= nil then
print(“playing now…”)
audio.play(sound_control.sound_table[effect])
end
end
return sound_control
Well you don’t need to require “audio”. Do you have an “audio.lua”? if so you should not overwrite the build in audio library.
You can also print out the value of effect and make sure it’s what you think your doing. How are you calling the function?
In main.lua, I create a global variable for the sounds.lua file so that I can call that function from anywhere. I assume that when I create that variable, that is when the sounds are loaded. To call the function:
sound_control.play_effect(“correct”)
When I call it, “playing now…” prints out every time, but it only plays the sound sometimes.
You might want to get a channel. See: http://docs.coronalabs.com/api/library/audio/findFreeChannel.html
and try playing the sound on that specific channel.
Rob
That worked. Thank you.