Jump to content

Fade sound away for some seconds


errsousa

Recommended Posts

Posted

Hi,

So i'm making a map that has a custom song (music.mp3) and at one point in the map i play a custom sound (music2.mp3 - 3seconds).

Unfortunately the second sound is not loud enough because of the bg music.

So, can someone please provide me a simple script to fade out music.mp3 when music2.mp3 plays and then fade in music.mp3 again?

I'm using this script for the custom sound (music2.mp3 -3 seconds).

--speedboosts-- 
    function start() 
        SpeedBoost = {  
                    createMarker ( -1906.3000488281, -5613.7001953125, 117.30000305176, "corona", 0, 0, 0, 0, 255), 
                }                                                         
      
    end 
    addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), start ) 
                                      
      
    Multiplier = 1 
      
    function SpeedBoost (player) 
        if player ~= getLocalPlayer() then return end 
        if getElementType(player) == "player" then 
            if isPedInVehicle ( player ) then 
                local car = getPedOccupiedVehicle(player) 
                if getVehicleController(car) == player then 
                    speedx, speedy, speedz = getElementVelocity ( car ) 
                    speedcnx = (speedx*Multiplier) 
                    speedcny = (speedy*Multiplier) 
                    speedcnz = (speedz*Multiplier) 
                    setElementVelocity ( car, speedcnx, speedcny,speedcnz ) 
            sound = playSound("files/liberty.mp3") 
                setSoundVolume(sound, 1)  
                end 
            end 
        end 
    end 
    addEventHandler ( "onClientMarkerHit", getResourceRootElement(getThisResource()), SpeedBoost ) 

Posted

Despite the fact that maps with big mp3 files suck, I can't find neither music.mp3 nor music2.mp3 in your script.

Just set the volume of that song to zero where you use playSound on the other one.

Posted

2nd song is in another script.

Is it possible to put both in the same script?

song 1 starts with map and fades out when song 2 comes in (can be by a cp, a marker, wtv) - song 2 fades out and song 1 comes back again.

Posted

Unstested though.

local song = playSound("music3.mp3") -- your song 
local turn = true -- unfaded by default 
  
function startFading() 
  turn = false 
end 
  
function stopFading() 
  turn = true 
end 
  
function renderFade() 
  local sounds = getElementsByType("sound") 
  for i,v in ipairs(sounds) do 
    if not (v == song) then -- if it's not your song then 
      local volume = getSoundVolume(v) 
      if turn == true then 
        volume = volume + 0.03 -- fade in 3% each frame 
      else 
        volume = volume - 0.03 -- fade out 3% each frame 
      end 
      if volume > 1.0 then -- volume can't exceed 100% 
        volume = 1.0 
      elseif volume < 0.2 then -- your minimal volume 
        volume = 0.2 
      end 
      setSoundVolume(v,volume) 
    end 
  end 
end 
addEventHandler("onClientRender",getRootElement(),renderFade) 

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...