audio.play with table values

Hi guys,

I play sounds in my app by doing this

local eatsound = { “sounds/eat1.wav”, “sounds/eat2.wav”, “sounds/eat3.wav”}

Basically creating a table set of strings. Which I then play by doing this.

local choice = math.random( 1, 3 )
media.playEventSound( swingsound[choice] )

This works fine. But reading the docs they reccomend playing sound by doing audio.play()

However it doesn’t seem to take in values passed via the table… so this doesn’t work:

local choice = math.random( 1, 3 )
audio.play ( swingsound[choice] )

Is there anyway to do this? Or am I just being anal retentive in following the recommendations from the API docs?

Thank you. [import]uid: 130916 topic_id: 25417 reply_id: 325417[/import]

Hi,

I haven’t tried this but do you need to add audio.loadSound() prior to audio.play()?

ie.

audio.loadSound(swingsound[choice] )–add this line
audio.play ( swingsound[choice] )

Just a thought…

Cheers

Nick [import]uid: 50787 topic_id: 25417 reply_id: 102678[/import]

Hi damian.gilbert,
I think the issue is, you have not declared array of swingsound. [import]uid: 75428 topic_id: 25417 reply_id: 102683[/import]

Hi nicholas.windsor, adding

audio.loadSound(swingsound[choice] )
audio.play ( swingsound[choice] )

Did not change anything. No sound plays in the simulator. [import]uid: 130916 topic_id: 25417 reply_id: 102686[/import]

I don’t understand what you mean…

I declare the table as local eatsound = { “sounds/eat1.wav”, “sounds/eat2.wav”, “sounds/eat3.wav”}

What do you think I need to do? [import]uid: 130916 topic_id: 25417 reply_id: 102687[/import]

I mean when you are entering in screen load all the table sound
ex: for i =1,3 do
audio.loadSound(eatsound[i])
end
and then play your sound file.
local choice=math.random(1,3)
audio.play(eatsound[choice]) [import]uid: 75428 topic_id: 25417 reply_id: 102689[/import]

That was the issue, you can’t play a sound without first loading it using either

audio.loadSound()  
--or  
audio.loadStream()  

You are right to use the new (openAL) api’s, as the eventsound api’s are extremely depreciated.

Also, please use code tags (everyone). It makes your code easier to read.

Example of how to use code tags:

<code>
Put your code here
</code> [import]uid: 84637 topic_id: 25417 reply_id: 102697[/import]

Thanks Sabir. I managed to fix it by doing essentially the same.

 local choice = math.random( 1, 3 )  
 local path = audio.loadSound(swingsound[choice])  
 audio.play ( path )  

The sounds are really really short so load time is very small.

Going to test on iOS if this works correctly. [import]uid: 130916 topic_id: 25417 reply_id: 102691[/import]

I managed to fix it for now.

 local choice = math.random( 1, 3 )  
 local path = audio.loadSound(swingsound[choice])  
 audio.play ( path )  

This works. Seems like loading the sound and then passing the path was the issue.

Thanks everyone! [import]uid: 130916 topic_id: 25417 reply_id: 102688[/import]

Thanks!

It works great on iOS and Android. [import]uid: 130916 topic_id: 25417 reply_id: 102715[/import]

I just glanced through this thread but I think you might find yourself with memory problems with this method.

Instead of

[lua] local choice = math.random( 1, 3 )
local path = audio.loadSound(swingsound[choice])
audio.play ( path )[/lua]

Try

[lua]local swingsound = { audio.loadSound(“1.wav”, audio.loadSound(“2.wav”), audio.loadSound(“3.wav”) }

audio.play( swingsound[math.random(3)] )[/lua]

The point being you know exactly where all your audiohandle references live so you can properly dispose of them when you need to:

[lua]for i=1,3 do
swingsound[i]:dispose()
end
swingsound=nil[/lua] [import]uid: 44647 topic_id: 25417 reply_id: 102728[/import]