Jump to content

PlaySound - Queue


'LinKin

Recommended Posts

Hello,

I'm using playSound function. So it plays a sound when something happens in game, but, there exists the possibility that 2 events are triggered with a veery small difference of time, so I think that the sounds would mix up. Meaning that the player will not hear clearly.

How could I avoid this?

Like, just when it finishes playing the first sound, it starts to play the other one.

Thanks.

Link to comment
  • Moderators

Try this:

local soundQueue = {} 
local currentSound 
local canPlay = true 
  
function playLatentSound( path ) 
    if not path or type( path ) ~= "string" then return end 
    local snd = playSound( path ) 
    if not canPlay then 
        setSoundPaused( snd, true ) 
        table.insert( soundQueue, snd ) 
    else 
        currentSound = snd 
        canPlay = false 
    end 
end 
  
function whenSoundStops ( ) 
    if source ~= currentSound or #soundQueue < 1 then return end 
    local nextSnd = soundQueue[1] 
    if nextSnd and getElementType( nextSnd ) == "sound" then 
        setSoundPaused( nextSnd, false ) 
        table.remove( soundQueue, 1 ) 
        currentSound = nextSound 
    else 
        canPlay = true 
    end        
end 
addEventHandler( "onClientSoundStop", root, whenSoundStops) 

Use playSoundLatent instead of playSound.

Note that this system obviously won't support the looped argument.

(I wrote everything from my phone. Took me a lot of time but I hope it will work.)

I just got another idea if you need to prevent the sound mixing for all sounds (even those that are coming from other resources.

Link to comment

Oh well, Ok

But I've done it this way: And I think it's pretty the same..

  
addEventHandler("onClientElementDestroy", root, 
function() 
    if source == currentSound then 
        canPlay = true 
        if #soundQueue >= 1 then  
            local nextSnd = soundQueue[1] 
            if nextSnd and getElementType( nextSnd ) == "sound" then 
                setSoundPaused( nextSnd, false ) 
                table.remove( soundQueue, 1 ) 
                currentSound = nextSnd 
            end          
        end 
    end 
end) 
  

Took it from here: https://forum.multitheftauto.com/viewtopic.php?f ... 3&p=398931

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...