Jump to content

problem whit sounds


Snoop.Cat

Recommended Posts

hey guys i have a problem , when i start this music script the other music from the map resources keep playing , my question is how i can mute the music from every map that start so players can only hear the music from my resource?

here is my resource:

function startMusic() 
    setRadioChannel(0) 
    song = playSound("song.mp3",true) 
end 
  
function makeRadioStayOff() 
    setRadioChannel(0) 
    cancelEvent() 
end 
  
function toggleSong() 
    if not songOff then 
        setSoundVolume(song,0) 
        songOff = true 
        removeEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) 
    else 
        setSoundVolume(song,1) 
        songOff = false 
        setRadioChannel(0) 
        addEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) 
    end 
end 
  
addEventHandler("onClientResourceStart",getResourceRootElement(getThisResource()),startMusic) 
addEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) 
addEventHandler("onClientPlayerVehicleEnter",getRootElement(),makeRadioStayOff) 
addCommandHandler("music",toggleSong) 
bindKey("m","down","music") 

Link to comment

soo code should be like

   
for i, sound in ipairs(getElementsByType("sound")) do 
setSoundVolume(sound, 0) 
end 
  
  function startMusic() 
        setRadioChannel(0) 
        song = playSound("song.mp3",true) 
    end 
      
    function makeRadioStayOff() 
        setRadioChannel(0) 
        cancelEvent() 
    end 
      
    function toggleSong() 
        if not songOff then 
            setSoundVolume(song,0) 
            songOff = true 
            removeEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) 
        else 
            setSoundVolume(song,1) 
            songOff = false 
            setRadioChannel(0) 
            addEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) 
        end 
    end 
      
    addEventHandler("onClientResourceStart",getResourceRootElement(getThisResource()),startMusic) 
    addEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) 
    addEventHandler("onClientPlayerVehicleEnter",getRootElement(),makeRadioStayOff) 
    addCommandHandler("music",toggleSong) 
    bindKey("m","down","music") 
  

and then only this sound of this specific script will play and others from maps will be muted always?

Link to comment

Oh... :D My bad...

  
function muteSounds () 
for i, sound in ipairs(getElementsByType("sound")) do 
if not sound == song then 
setSoundVolume(sound, 0) 
end 
end 
end 
  
addEventHandler("onClientResourceStart", root,  
function () 
setTimer(muteSounds, 1000, 1) --To make sure that the sound is already playing 
end) 
  

OR

You could simply use timer..

  
function muteSounds () 
for i, sound in ipairs(getElementsByType("sound")) do 
if not sound == song then 
setSoundVolume(sound, 0) 
end 
end 
end 
  
setTimer(muteSounds, 1000, 0) 
  

Link to comment

You need to use onClientResourceStart. I recommend you do not mute the sound, this can cause lag as it still uses background CPU.

Example:

-- This script requires mapmanager to be running!

addEventHandler('onClientResourceStart', getRootElement(), 
function(that) 
    if that ~= this and exports.mapmanager:isMap(that) then 
        for i, v in ipairs(getElementsByType("sound")) do 
            stopSound(v) 
        end 
        startMusic() 
    end 
end ) 
  
function startMusic() 
    setRadioChannel(0) 
    song = playSound("song.mp3",true) 
end 
  
function makeRadioStayOff() 
    setRadioChannel(0) 
    cancelEvent() 
end 
  
function toggleSong() 
    if not songOff then 
        setSoundVolume(song,0) 
        songOff = true 
        removeEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) 
    else 
        setSoundVolume(song,1) 
        songOff = false 
        setRadioChannel(0) 
        addEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) 
    end 
end 
  
addEventHandler("onClientPlayerRadioSwitch",getRootElement(), makeRadioStayOff) 
addEventHandler("onClientPlayerVehicleEnter",getRootElement(), makeRadioStayOff) 
addCommandHandler("music", toggleSong) 
bindKey("m","down","music") 

Link to comment
You need to use onClientResourceStart. I recommend you do not mute the sound, this can cause lag as it still uses background CPU.

Example:

-- This script requires mapmanager to be running!

addEventHandler('onClientResourceStart', getRootElement(), 
function(that) 
    if that ~= this and exports.mapmanager:isMap(that) then 
        for i, v in ipairs(getElementsByType("sound")) do 
            stopSound(v) 
        end 
        startMusic() 
    end 
end ) 
  
function startMusic() 
    setRadioChannel(0) 
    song = playSound("song.mp3",true) 
end 
  
function makeRadioStayOff() 
    setRadioChannel(0) 
    cancelEvent() 
end 
  
function toggleSong() 
    if not songOff then 
        setSoundVolume(song,0) 
        songOff = true 
        removeEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) 
    else 
        setSoundVolume(song,1) 
        songOff = false 
        setRadioChannel(0) 
        addEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) 
    end 
end 
  
addEventHandler("onClientPlayerRadioSwitch",getRootElement(), makeRadioStayOff) 
addEventHandler("onClientPlayerVehicleEnter",getRootElement(), makeRadioStayOff) 
addCommandHandler("music", toggleSong) 
bindKey("m","down","music") 

it doesnt works :/ it doesnt start music from resource and not mute the music from maps :/

Link to comment

Sorry small mistake, its now working:

addEventHandler('onClientResourceStart', getRootElement(), 
function(that) 
    if that ~= this then 
        for i, v in ipairs(getElementsByType("sound")) do 
            stopSound(v) 
        end 
        startMusic() 
    end 
end ) 
  
function startMusic() 
    setRadioChannel(0) 
    song = playSound("song.mp3",true) 
end 
  
function makeRadioStayOff() 
    setRadioChannel(0) 
    cancelEvent() 
end 
  
function toggleSong() 
    if not songOff then 
        setSoundVolume(song,0) 
        songOff = true 
        removeEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) 
    else 
        setSoundVolume(song,1) 
        songOff = false 
        setRadioChannel(0) 
        addEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) 
    end 
end 
  
addEventHandler("onClientPlayerRadioSwitch",getRootElement(), makeRadioStayOff) 
addEventHandler("onClientPlayerVehicleEnter",getRootElement(), makeRadioStayOff) 
addCommandHandler("music", toggleSong) 
bindKey("m","down","music") 

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