Jump to content

[HELP] Playing Sound Server-Sided


justn

Recommended Posts

Hi, i have this radio script, but i want the song to play server-sided so that everyone can here it.

triggerServerEvent can be found on line 88

Client

n = 1 
Font = dxCreateFont("font.ttf", 21) 
local radios = { 
    {"None","None",7}, 
    {"http://www.raggakings.net/listen.m3u","Reggae and Dancehall",6}, 
    {"http://mp3uplink.duplexfx.com:8054/listen.pls","West Coast Rap",5}, 
    {"http://somafm.com/dubstep.pls","Dubstep",4}, 
    {"http://www.181.fm/winamp.pls?station=181-power&style=mp3&description=Power%20181%20(Top%2040)&file=181-power.pls","Power 181",3}, 
    {"http://193.34.51.25/listen.pls","rauteMusik.",2}, 
    {"http://212.45.104.34:8042/listen.pls","Defjay",1} 
} 
  
title = "Radio Off" 
addEventHandler("onClientSoundChangedMeta", root, function(streamTitle) 
    title = streamTitle 
end) 
addEventHandler("onClientSoundStream",root,function(suc,length,streamN) 
    title = streamN 
end) 
  
function onPlayerEnterVehicle(vehicle,seat) 
if ( source == localPlayer ) and isPedInVehicle(localPlayer) then 
    if seat == 0 then 
        setRadioChannel(0) 
        addEventHandler("onClientRender", getRootElement(), RenderClient) 
        bindKey("mouse_wheel_up", "down", PlaySound, "up") 
        bindKey("mouse_wheel_down", "down", PlaySound, "down") 
        bindKey("r", "down", PlaySound, "up") 
    elseif seat == 1 or seat == 2 or seat == 3 or seat == 4 then 
        addEventHandler("onClientRender", getRootElement(), RenderClient) 
        setRadioChannel(0) 
        bindKey("mouse_wheel_up", "down", ClearSounds, "up") 
        bindKey("mouse_wheel_down", "down", ClearSounds, "down") 
        bindKey("r", "down", ClearSounds, "up") 
    end 
end 
end 
addEventHandler("onClientPlayerVehicleEnter", getRootElement(), onPlayerEnterVehicle) 
  
function stopMySound(vehicle,seat) 
if ( source == localPlayer ) and not isPedInVehicle(localPlayer) then 
    if seat == 0 or seat == 1 or seat == 2 or seat == 3 or seat == 4  then 
        if sound then 
            stopSound( sound ) 
            sound = nil 
        end 
        removeEventHandler("onClientRender", getRootElement(), RenderClient) 
        unbindKey("mouse_wheel_up", "down", PlaySound) 
        unbindKey("mouse_wheel_down", "down", PlaySound) 
        unbindKey("r", "down", PlaySound) 
        n = 1 
    end 
    end 
end 
addEventHandler("onClientPlayerVehicleExit",getRootElement(),stopMySound) 
  
function RenderClient() 
    if isPedInVehicle(localPlayer) then 
        local x,y = guiGetScreenSize() 
        dxDrawText("Song Name: "..title, x*387/1024, y*631/768, x*800/1024, y*646/768, tocolor(255, 255, 255, 255), 0.50, Font, "left", "top", false, false, true, false, false) 
        dxDrawText("Radio: "..tostring(radiotitle), x*387/1024, y*616/768, x*760/1024, y*631/768, tocolor(255, 255, 255, 255), 0.50, Font, "left", "top", false, false, true, false, false) 
    end 
end 
  
function PlaySound(state) 
    local x,y,z = getElementPosition(getLocalPlayer()) 
    n = tonumber(n) 
    if sound then 
    stopSound(sound) 
    setRadioChannel(0) 
    end 
    if state == "mouse_wheel_up" then 
        if n < #radios then 
            n = n + 1 
            setRadioChannel(0) 
        end 
    elseif state == "mouse_wheel_down" then 
        if n > 1 then 
            n = n - 1 
            setRadioChannel(0) 
        end 
    elseif state == "r" then 
    if n < #radios then 
    n = n + 1 
    setRadioChannel(0) 
    end 
    end 
    triggerServerEvent("playSound",localPlayer, HERE ) 
    --sound = playSound3D(radios[n][1], x,y,z) 
    radiotitle = radios[n][2] 
    setRadioChannel(0) 
end  
  
function UpdateSound() 
    if sound then 
        local x,y,z = getElementPosition(getLocalPlayer()) 
        attachElements(sound, localPlayer) 
        --setElementPosition(sound, x,y,z) 
    end 
end 
addEventHandler("onClientRender", getRootElement(), UpdateSound) 
  
function ClearSounds(state) 
if state == "mouse_wheel_up" or state == "mouse_wheel_down" or state == "r" then 
setRadioChannel(0) 
end 
end 

Server

addEvent("playSound",true) 
addEventHandler("playSound",root, 
function() 
local x,y,z = getElementPosition(source) 
playSound3D( HERE , x,y,z) 
end) 

Link to comment

playSound3D is a client side only function, you can't use it in the server side.

The only way to sync the sound for everyone is to trigger like you did, but instead of using playSound3D, you must trigger a client side event using triggerClientEvent to everyone, then in the client side you add the event to play the sound.

Link to comment

2 Problems

First: When i was listening to a station and a friend goes in a car and turns to the same station, we both hear the station get doubled. which means it plays two times.

Second: The music does not stop when i exit the vehicle

Client

n = 1 
Font = dxCreateFont("font.ttf", 21) 
local radios = { 
    {"None","None",8}, 
    {"http://www.raggakings.net/listen.m3u","Reggae and Dancehall",7}, 
    {"http://www.hit104.com/listen.pls", "Hit 104",6}, 
    {"http://mp3uplink.duplexfx.com:8054/listen.pls","West Coast Rap",5}, 
    {"http://www.181.fm/winamp.pls?station=181-power&style=mp3&description=Power%20181%20(Top%2040)&file=181-power.pls","Power 181",4}, 
    {"http://somafm.com/dubstep.pls","Dubstep",3}, 
    {"http://193.34.51.25/listen.pls","rauteMusik.",2}, 
    {"http://212.45.104.34:8042/listen.pls","Defjay",1} 
} 
  
title = "Radio Off" 
addEventHandler("onClientSoundChangedMeta", root, function(streamTitle) 
    title = streamTitle 
end) 
addEventHandler("onClientSoundStream",root,function(suc,length,streamN) 
    title = streamN 
end) 
  
function onPlayerEnterVehicle(vehicle,seat) 
if ( source == localPlayer ) and isPedInVehicle(localPlayer) then 
    if seat == 0 then 
        setRadioChannel(0) 
        addEventHandler("onClientRender", getRootElement(), RenderClient) 
        bindKey("mouse_wheel_up", "down", PlaySound, "up") 
        bindKey("mouse_wheel_down", "down", PlaySound, "down") 
        bindKey("r", "down", PlaySound, "up") 
    elseif seat == 1 or seat == 2 or seat == 3 or seat == 4 then 
        addEventHandler("onClientRender", getRootElement(), RenderClient) 
        setRadioChannel(0) 
        bindKey("mouse_wheel_up", "down", ClearSounds, "up") 
        bindKey("mouse_wheel_down", "down", ClearSounds, "down") 
        bindKey("r", "down", ClearSounds, "up") 
    end 
end 
end 
addEventHandler("onClientPlayerVehicleEnter", getRootElement(), onPlayerEnterVehicle) 
  
function stopMySound(vehicle,seat) 
if ( source == localPlayer ) and not isPedInVehicle(localPlayer) then 
    if seat == 0 or seat == 1 or seat == 2 or seat == 3 or seat == 4  then 
        if sound then 
            triggerServerEvent("pauseSound",localPlayer,sound) 
            sound = nil 
        end 
        removeEventHandler("onClientRender", getRootElement(), RenderClient) 
        unbindKey("mouse_wheel_up", "down", PlaySound) 
        unbindKey("mouse_wheel_down", "down", PlaySound) 
        unbindKey("r", "down", PlaySound) 
        n = 1 
    end 
    end 
    end 
addEventHandler("onClientPlayerVehicleExit",getRootElement(),stopMySound) 
  
function RenderClient() 
    if isPedInVehicle(localPlayer) then 
        local x,y = guiGetScreenSize() 
        dxDrawText("Song Name: "..title, x*387/1024, y*631/768, x*800/1024, y*646/768, tocolor(255, 255, 255, 255), 0.50, Font, "left", "top", false, false, true, false, false) 
        dxDrawText("Radio: "..tostring(radiotitle), x*387/1024, y*616/768, x*760/1024, y*631/768, tocolor(255, 255, 255, 255), 0.50, Font, "left", "top", false, false, true, false, false) 
    end 
end 
  
function PlaySound(state) 
    local x,y,z = getElementPosition(getLocalPlayer()) 
    n = tonumber(n) 
    if sound then 
    triggerServerEvent("pauseSound",localPlayer,sound) 
    setRadioChannel(0) 
    end 
    if state == "mouse_wheel_up" then 
        if n < #radios then 
            n = n + 1 
            setRadioChannel(0) 
        end 
    elseif state == "mouse_wheel_down" then 
        if n > 1 then 
            n = n - 1 
            setRadioChannel(0) 
        end 
    elseif state == "r" then 
    if n < #radios then 
    n = n + 1 
    setRadioChannel(0) 
    end 
    end 
    triggerServerEvent("playSound",localPlayer) 
    setRadioChannel(0) 
end  
  
  
function startSound() 
local x,y,z = getElementPosition(getLocalPlayer()) 
sound = playSound3D(radios[n][1],x,y,z) 
radiotitle = radios[n][2] 
setRadioChannel(0) 
end 
addEvent("startSound",true) 
addEventHandler("startSound",getRootElement(),startSound) 
  
function StopSound(sound) 
stopSound(sound) 
end 
addEvent("StopSound",true) 
addEventHandler("StopSound",getRootElement(),StopSound) 
  
function UpdateSound() 
    if sound then 
        local x,y,z = getElementPosition(getLocalPlayer()) 
        attachElements(sound, localPlayer) 
        --setElementPosition(sound, x,y,z) 
    end 
end 
addEventHandler("onClientRender", getRootElement(), UpdateSound) 
  
function ClearSounds(state) 
if state == "mouse_wheel_up" or state == "mouse_wheel_down" or state == "r" then 
setRadioChannel(0) 
end 
end 

Server

addEvent("playSound",true) 
addEventHandler("playSound",root, 
function() 
triggerClientEvent("startSound",root) 
end) 
  
addEvent("pauseSound",true) 
addEventHandler("pauseSound",root, 
function(sound) 
triggerClientEvent("StopSound",root,sound) 
end) 

Link to comment

I did that now, when i switch to one radio station, and then switch to another, both radio stations play at the same time. Also, when i exit the car with a radio station playing and i enter again. and i switch the radio station, both songs play at same time. :3

Link to comment

I have this code, it works but other players cant hear.

Client

n = 1 
Font = dxCreateFont("font.ttf", 21) 
local radios = { 
    {"None","None",8}, 
    {"http://www.raggakings.net/listen.m3u","Reggae and Dancehall",7}, 
    {"http://www.hit104.com/listen.pls", "Hit 104",6}, 
    {"http://mp3uplink.duplexfx.com:8054/listen.pls","West Coast Rap",5}, 
    {"http://www.181.fm/winamp.pls?station=181-power&style=mp3&description=Power%20181%20(Top%2040)&file=181-power.pls","Power 181",4}, 
    {"http://somafm.com/dubstep.pls","Dubstep",3}, 
    {"http://193.34.51.25/listen.pls","rauteMusik.",2}, 
    {"http://212.45.104.34:8042/listen.pls","Defjay",1} 
} 
title = "Radio Off" 
addEventHandler("onClientSoundChangedMeta", root, function(streamTitle) 
    title = streamTitle 
end) 
addEventHandler("onClientSoundStream",root,function(suc,length,streamN) 
    title = streamN 
end) 
  
function onPlayerEnterVehicle(vehicle,seat) 
if ( source == localPlayer ) then 
    if seat == 0 or seat == 1 or seat == 2 or seat == 3 or seat == 4 then 
        setRadioChannel(0) 
        addEventHandler("onClientRender", getRootElement(), RenderClient) 
        bindKey("mouse_wheel_up", "down", PlaySound, "up") 
        bindKey("mouse_wheel_down", "down", PlaySound, "down") 
    end 
end 
end 
addEventHandler("onClientPlayerVehicleEnter", getRootElement(), onPlayerEnterVehicle) 
  
function stopMySound(vehicle,seat) 
if ( source == localPlayer ) then 
    if seat == 0 or seat == 1 or seat == 2 or seat == 3 or seat == 4  then 
        if sound then 
         triggerServerEvent("pauseSound",localPlayer) 
        end 
        removeEventHandler("onClientRender", getRootElement(), RenderClient) 
        unbindKey("mouse_wheel_up", "down", PlaySound) 
        unbindKey("mouse_wheel_down", "down", PlaySound) 
        n = 1 
    end 
    end 
end 
addEventHandler("onClientPlayerVehicleExit",getRootElement(),stopMySound) 
  
function RenderClient() 
    if isPedInVehicle(localPlayer) then 
        local x,y = guiGetScreenSize() 
        dxDrawText("Song Name: "..title, x*387/1024, y*631/768, x*800/1024, y*646/768, tocolor(255, 255, 255, 255), 0.50, Font, "left", "top", false, false, true, false, false) 
        dxDrawText("Radio: "..tostring(radiotitle), x*387/1024, y*616/768, x*760/1024, y*631/768, tocolor(255, 255, 255, 255), 0.50, Font, "left", "top", false, false, true, false, false) 
    end 
end 
  
function PlaySound(state) 
    local x,y,z = getElementPosition(getLocalPlayer()) 
    n = tonumber(n) 
    if sound then     
    triggerServerEvent("pauseSound",localPlayer) 
        setRadioChannel(0) 
    end 
    if state == "mouse_wheel_up" then 
        if n < #radios then 
            n = n + 1 
            setRadioChannel(0) 
        end 
    elseif state == "mouse_wheel_down" then 
        if n > 1 then 
            n = n - 1 
            setRadioChannel(0) 
        end 
    end 
    triggerServerEvent("playSound",localPlayer,x,y,z) 
    setRadioChannel(0) 
end 
  
function UpdateSound() 
    if sound then 
    attachElements(sound,getPedOccupiedVehicle(getLocalPlayer())) 
    end 
end 
addEventHandler("onClientRender", getRootElement(), UpdateSound) 
  
addEvent("startSound",true) 
addEventHandler("startSound",root, 
function(x,y,z) 
sound = playSound3D(radios[n][1], x,y,z) 
radiotitle = radios[n][2] 
end) 
  
addEvent("StopSound",true) 
addEventHandler("StopSound",root, 
function() 
stopSound(sound) 
sound = nil 
end) 

Server

addEvent("playSound",true) 
addEventHandler("playSound",root, 
function(x,y,z) 
triggerClientEvent("startSound",root,x,y,z) 
end) 
  
addEvent("pauseSound",true) 
addEventHandler("pauseSound",root, 
function() 
triggerClientEvent("StopSound",root) 
end) 

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