In my current project there are a limited number of sounds that will be playing. For example when the player is firing there needs to be only one sound playing, the sound of the player firing. So there seem to be two ways to accomplish this..
1. Create a new sound channel every time the user presses fire.
2. Create a single sound and restart it every time the user presses fire.
On the surface it seems logical that creating one channel and reusing it over and over would be better than creating new objects over and over. So I put it to the test and the results were interesting to say the least...
- You can reuse a channel as long as it is still currently playing.
- As soon as a channel has finished playing you can not restart it at all.
Here's some code..
local sound = Sound.new("someSound.wav") -- Load sound sample
local effect = sound:play(0, false, true) -- Create channel, non looping, paused
local function onTouched(e)
print(effect, "position = ", effect:getPosition()) -- Dump effect and it's current play position to console
if effect:isPaused() then
effect:setPaused(false) -- Resume
elseif effect:isPlaying() then
effect:setPosition(0) -- Restart
else
effect = sound:play(0, false, false) -- It can't be restarted, create another
end
end
stage:addEventListener(Event.TOUCHES_BEGIN, onTouched) |
In the code above you can tap the screen which will restart the sound, as long as it has not finished playing. Once it has finished playing a new channel object needs to be created.
So this is more efficient than creating a new channel object every time the player presses fire but it would still be preferable to be able to restart a channel that has already finished. Does anyone know how this might be accomplished?
Comments
Unfortunately when a channel is set to looping, it does not trigger an Event.COMPLETE event *doh*
Is it even possible to have an event raised when a channel loops?
Likes: totebo
Likes: antix
Sounds are a key element in pretty much any game you can think of and they should really perform a bit better.
I load all my sounds up at the beginning and play them at will.
eg, something like this (this is untested code, just making it up here!)...
The routine also makes it so that if for some reason the allocation of the channel fails, then it will be retried then next game loop until it plays.
Maybe something like this would work to replace the "if channel..." stuff above:
You could improve the routine by having a counter decrement so that there will only be x retries, eg you would then have something like this in your code:
Likes: antix
https://deluxepixel.com
However it is still creating a new channel every time a sound is played (or so it seems). What I would like to do is create one channel and just keep reusing it.
Still, I think a merging of methods would be pretty cool too. I'll work on that a bit later this evening.
Likes: SinisterSoft
https://deluxepixel.com
The first time a sound effect is played a new channel is created. Each subsequent time the sound effect is played it restarts the channel if it is already playing and creates a new channel if it has completed. There is more logic but it seems more elegant to me.
(please)
The problem if you use the same sound channel if the same sound needs to play again, but hasn't finished is that the sound resets - it sounds good for a gimmick effect, but sounds odd if used for a normal game sound.
https://deluxepixel.com
This 'effect' can be used as a gimmick effect though - like I mentioned I used it on a previous game so that if the sound was still playing I made the restarted sound faster and louder - so if it happened again it went faster again, etc... If the sound stopped playing then the speed was reset and the volume dropped.
Imagine the tae tae tae tae on respectable by mel and kim...
Likes: talis
https://deluxepixel.com
I suppose I will have to rethink my sound effect class to accommodate for all of these interesting types of effect.
Likes: SinisterSoft, talis
Likes: SinisterSoft
Likes: SinisterSoft, simwhi