I have a routine that produces a random handle for audio files. When I call this in audio.play as (stringname[randNumber]), which equals a random handle, nothing happens. There are no errors, nothing. If I print
(stringname[randNumber]) I get the handle of the audio file that is supposed to be playing
All the audio files are loaded and will play with audio.play(handle).
What am I forgetting?? [import]uid: 42083 topic_id: 25353 reply_id: 325353[/import]
There isn’t enough information here.
audio.play simply expects a handle created by audio.loadSound or loadStream. It doesn’t care about the intermediate functions you use to randomize it. Your obligation is that your random functions must actually return a valid handle.
My guess is you have a bug in your random function. You should print out the types and addresses of what your random function is returning and compare it to the addresses of known audio handles to make sure they match.
[import]uid: 7563 topic_id: 25353 reply_id: 102467[/import]
Thanks, I got it figured out.
I was trying to call audio.play(stringname) but where audio.loadSound accepts a string, audio.play does NOT accept a string.
so I had this:
audio1 = audio.loadSound(“soundFile1.wav”)
audio2 = audio.loadSound(“soundFile2.wav”)
audio3 = audio.loadSound(“soundFile3.wav”)
etc.
and then I was picking a sound to play at random (as a string - either “audio1” or “audio2” etc.) and trying to pass it into audio.play
if (randNumber == 1) then
randomString = string.format(“audio1”)
elseif (randNumber == 2) then
randomString = string.format(“audio2”)
etc.
Then I would try to play the randomly chosen sound using:
audio.play(randomString)
Even though the string I was passing in had the exact same name as the loadSound handle I created, audio.play didn’t know that I meant for it to play the handle with the same name as the string I was passing in.
To get this working, instead I ended up doing something like:
if (randNumber == 1) then
audio1 = audio.loadSound(“soundFile1.wav”)
elseif (randNumber == 2) then
audio1 = audio.loadSound(“soundFile2.wav”)
etc.
and then:
audio.play(audio1)
[import]uid: 42083 topic_id: 25353 reply_id: 102705[/import]
Yeah, you have to pass the audio handle which you loaded the audio into to audio.play, it doesn’t accept strings.
However if you want to do it like that, here is an example of a good way to handle it
local audioList = {
["sound1"] = audio.loadSound("soundfile1.wav"),
["sound2"] = audio.loadSound("soundfile2.wav")
}
--Then you can easily reference a table key value to play your audio
audio.play(audioList["sound2"])
Hope that helps [import]uid: 84637 topic_id: 25353 reply_id: 102720[/import]