sample Event Sound code only playing once

I’m doing my best to copy the sample code for event sound, but even though the timer is working fine, I can only get the sound itself to play once.

My print statement is successfully running every second, so I know it’s hitting the function

I’m playing a 30ms file, and I’ve tried both .wav and .mp3.

I’m running build 2011.704 on a Windows machine

Here’s the sample code I’ve copied from http://developer.anscamobile.com/content/multimedia-features

[lua]local soundID = media.newEventSound( “beep.caf” )

local playBeep = function()
media.playEventSound( soundID )
end
timer.performWithDelay( 1000, playBeep, 0 )[/lua]

And here’s my code:

[lua]local soundID = media.newEventSound( “sounds/klack_wav.wav” )

local playBeep = function()
print( “playBeep called”)
media.playEventSound( soundID )
end

timer.performWithDelay( 1000, playBeep, 0 )[/lua]

Any ideas? Thanks in advance! [import]uid: 81015 topic_id: 20033 reply_id: 320033[/import]

Please don’t use the old audio API and use the new audio API instead.
local soundID = audio.loadSound( “sounds/klack_wav.wav”)

local playBeep = function()
audio.play( soundID )
end

timer.performWithDelay( 1000, playBeep, 0 )
[import]uid: 7563 topic_id: 20033 reply_id: 78227[/import]

Thank you so much for the prompt reply!

Using the above code, unfortunately I’m just getting a

WARNING: failed to create audio sound (sounds/klack\_wav.wav)  

from the terminal window.

EDIT: It looks like the problem now was that there was something weird with my .wav encode. I tried it with an .mp3, and everything works great. Thanks again for your help! [import]uid: 81015 topic_id: 20033 reply_id: 78315[/import]

I’m having this same problem, when using media.playEventSound() it only plays the first time. Due to the time delay on android I need to use the old API.

Any suggestions?

It appears this only happens in the simulator and works fine on the device. [import]uid: 45650 topic_id: 20033 reply_id: 134515[/import]

As Eric said in reply #1, don’t use media.* to play audio sounds (unless you’re playing a podcast) instead use the audio.* API calls. They work much better. [import]uid: 199310 topic_id: 20033 reply_id: 134561[/import]

For most of my app I am using audio.*, but on android there’s a substantial delay between when the function is called and the sound actually plays, so I’ve been using media.* for situations where instant playback is required. There’s a warning on the audio.play() which says:

“NOTE: A known issue with Android causes sound to be delayed by 1 to 2 seconds when started.”

From what I can tell that delay doesn’t happen using media.*. If there’s a way to use audio.* without the delay that would be preferred. [import]uid: 45650 topic_id: 20033 reply_id: 134578[/import]

The [lua]media.playEventSound()[/lua] function has the least latency on Android. The reason it is the fastest is because it is loaded straight to the audio hardware versus the [lua]audio[/lua] API goes through a middle software layer (OpenAL) that is CPU bound which is streamed to the hardware. That middle layer adds to the latency because it is CPU bound, although it looks like Android OS 4.2 adds hardware support and is significantly faster. In any case, that is the technical reasons why one API is faster than the other.

At the moment, I highly recommend that you use the [lua]media.playEventSound()[/lua] API to play sound effects, but only on Android because that API has the issue mentioned up in the Corona Simulator and iOS. That function does not have these issues on Android. For music or long playing sounds where you don’t care about the latency, the [lua]audio[/lua] API is best because it offers the most capabilities (volume control and the ability to stream from file).

We do realize that using the media API only on Android to play short sound effects is kind of a pain. In the future, we want to look into updating the audio API to also specialize in playing short low latency sound effects for all platforms so that you only have to code it once. [import]uid: 32256 topic_id: 20033 reply_id: 134657[/import]

Thanks, that’s what I’m doing. One thing I couldn’t figure out, how do I release the memory used by event sounds? I don’t see a function to do that, do I just keep the sound loaded until the app quits? Or does the garbage collector free it up if I set the variable to nil?

thanks [import]uid: 45650 topic_id: 20033 reply_id: 134723[/import]

The [lua]media.newEventSound()[/lua] function returns an object used to identify the loaded sound. When you give up all references to that returned object, then that audio clip will be automatically freed from memory once Lua has garbage collected it. [import]uid: 32256 topic_id: 20033 reply_id: 134727[/import]

Perfect, thanks! [import]uid: 45650 topic_id: 20033 reply_id: 134729[/import]

I’m having this same problem, when using media.playEventSound() it only plays the first time. Due to the time delay on android I need to use the old API.

Any suggestions?

It appears this only happens in the simulator and works fine on the device. [import]uid: 45650 topic_id: 20033 reply_id: 134515[/import]

As Eric said in reply #1, don’t use media.* to play audio sounds (unless you’re playing a podcast) instead use the audio.* API calls. They work much better. [import]uid: 199310 topic_id: 20033 reply_id: 134561[/import]

For most of my app I am using audio.*, but on android there’s a substantial delay between when the function is called and the sound actually plays, so I’ve been using media.* for situations where instant playback is required. There’s a warning on the audio.play() which says:

“NOTE: A known issue with Android causes sound to be delayed by 1 to 2 seconds when started.”

From what I can tell that delay doesn’t happen using media.*. If there’s a way to use audio.* without the delay that would be preferred. [import]uid: 45650 topic_id: 20033 reply_id: 134578[/import]

The [lua]media.playEventSound()[/lua] function has the least latency on Android. The reason it is the fastest is because it is loaded straight to the audio hardware versus the [lua]audio[/lua] API goes through a middle software layer (OpenAL) that is CPU bound which is streamed to the hardware. That middle layer adds to the latency because it is CPU bound, although it looks like Android OS 4.2 adds hardware support and is significantly faster. In any case, that is the technical reasons why one API is faster than the other.

At the moment, I highly recommend that you use the [lua]media.playEventSound()[/lua] API to play sound effects, but only on Android because that API has the issue mentioned up in the Corona Simulator and iOS. That function does not have these issues on Android. For music or long playing sounds where you don’t care about the latency, the [lua]audio[/lua] API is best because it offers the most capabilities (volume control and the ability to stream from file).

We do realize that using the media API only on Android to play short sound effects is kind of a pain. In the future, we want to look into updating the audio API to also specialize in playing short low latency sound effects for all platforms so that you only have to code it once. [import]uid: 32256 topic_id: 20033 reply_id: 134657[/import]

Thanks, that’s what I’m doing. One thing I couldn’t figure out, how do I release the memory used by event sounds? I don’t see a function to do that, do I just keep the sound loaded until the app quits? Or does the garbage collector free it up if I set the variable to nil?

thanks [import]uid: 45650 topic_id: 20033 reply_id: 134723[/import]

The [lua]media.newEventSound()[/lua] function returns an object used to identify the loaded sound. When you give up all references to that returned object, then that audio clip will be automatically freed from memory once Lua has garbage collected it. [import]uid: 32256 topic_id: 20033 reply_id: 134727[/import]

Perfect, thanks! [import]uid: 45650 topic_id: 20033 reply_id: 134729[/import]