How to prevent a sound from playing back again while it's previous play was not finished.

Hi,

I have two objects that share one sound effect and on some criteria they play that sound effect. What I want to achieve is to prevent the playback of that sound effect when the other object is playing it.

Currently I preload that sfx in both objects and I think it makes things more complicated to manage.

Thanks. [import]uid: 206803 topic_id: 35840 reply_id: 335840[/import]

You would want to reserve a channel just for that sound. Then before playing the sound on that channel, check if the channel is still playing. If it’s not, then go ahead and play the sound, otherwise don’t. Having the sound in a single object isn’t neccessary.

Something like this:

[code]
function PlaySound(snd, chan)
if chan ~= nil then – check for a channel parameter
if audio.isChannelPlaying(chan) == false then – check if channel is playing
audio.play(snd, {channel=chan}) – play on specific channel
end
else
audio.play(snd) – no channel parameter, play like normal
end
end

audio.reserveChannels(1) – channel 1 is now locked for specific usage

mySound1 = audio.loadSound(sound1.mp3)
mySound2 = audio.loadSound(sound1.mp3)

PlaySound(mySound1,1) – will play like normal
PlaySound(mySound2,1) – will do nothing because mySound1 is still playing

[code] [import]uid: 56820 topic_id: 35840 reply_id: 142558[/import]

You would want to reserve a channel just for that sound. Then before playing the sound on that channel, check if the channel is still playing. If it’s not, then go ahead and play the sound, otherwise don’t. Having the sound in a single object isn’t neccessary.

Something like this:

[code]
function PlaySound(snd, chan)
if chan ~= nil then – check for a channel parameter
if audio.isChannelPlaying(chan) == false then – check if channel is playing
audio.play(snd, {channel=chan}) – play on specific channel
end
else
audio.play(snd) – no channel parameter, play like normal
end
end

audio.reserveChannels(1) – channel 1 is now locked for specific usage

mySound1 = audio.loadSound(sound1.mp3)
mySound2 = audio.loadSound(sound1.mp3)

PlaySound(mySound1,1) – will play like normal
PlaySound(mySound2,1) – will do nothing because mySound1 is still playing

[code] [import]uid: 56820 topic_id: 35840 reply_id: 142558[/import]