Jump to content

[HELP] How to sync the sound for all players?


Tokio

Recommended Posts

This is the code:

requestBrowserDomains({"www.convertmp3.io"})
local browser = createBrowser( 0, 0, false )
local currentSound = {}

function start(_,link)
    fetch(link)
end
addCommandHandler("play",start)

function fetch(url)
    if (url) then
        fetchRemote("http://www.convertmp3.io/fetch/?format=JSON&video="..url, callback)
    end
end

function callback(data, error)
    if (error ~= 0) then
        return outputChatBox(error)
    end
    if (data == "ERROR") then
        return outputChatBox("data error")
    end
    local data = fromJSON("["..data.."]")
    if (data) then
        outputChatBox("Title: "..data.title)
        outputChatBox("Length: "..data.length)
        outputChatBox("Link: "..data.link)
        loadBrowserURL( browser, data.link  )
    end
end

addEventHandler( "onClientBrowserNavigate", browser, function( link )
        if not link:find("www.convertmp3.io") then
		local vehicle = getPedOccupiedVehicle ( localPlayer )
		local x, y, z = getElementPosition(vehicle)
            currentSound[localPlayer] = playSound3D( link, x, y, z )
			attachElements(currentSound[localPlayer],vehicle)
			setSoundMaxDistance(currentSound[localPlayer],30)
			setSoundVolume(currentSound[localPlayer],50)
        end
    end 
)

How to synchronise to all players?

Link to comment
2 minutes ago, Sorata_Kanda said:

You could create a server event that includes a loop where it triggers a client event playing the sound file for each player.

I tried with server trigger, but then play the sound for every players vehicle.. 

Link to comment
10 hours ago, KillerX said:

 Try

 


-- Client --

function start (_ , link )
    triggerServerEvent( 'play' , localPlayer , link )
end

addEvent( 'Play' , true )
addEventHandler( 'Play' , root ,
  function( link )
      fetch( link )
  end
)

-- Server --

addEvent( 'play' , true )

addEventHandler( 'play' , root , 
    function( link )
        triggerClientEvent( root , 'Play' , client , link )
    end
)

 

When i play the music, the song start in every players vehicle too..  :s And when i play music in my car, then it is not heard by others

Link to comment
1 minute ago, Mr.Loki said:

Post the changes you made using the triggers.
you are probably using localPlayer and not source from the play event

Client side:

requestBrowserDomains({"www.convertmp3.io"})
local browser = createBrowser( 0, 0, false )
local currentSound = {}

function start (_ , link )
    triggerServerEvent( 'play' , localPlayer , link )
end
addCommandHandler("p",start)

addEvent( 'Play' , true )
addEventHandler( 'Play' , root ,
  function( link )
      fetch( link )
  end
)

function fetch(url)
    if (url) then
        fetchRemote("http://www.convertmp3.io/fetch/?format=JSON&video="..url, callback)
    end
end

function callback(data, error)
    if (error ~= 0) then
        return outputChatBox(error)
    end
    if (data == "ERROR") then
        return outputChatBox("data error")
    end
    local data = fromJSON("["..data.."]")
    if (data) then
        outputChatBox("Title: "..data.title)
        outputChatBox("Length: "..data.length)
        outputChatBox("Link: "..data.link)
        loadBrowserURL( browser, data.link  )
    end
end

addEventHandler( "onClientBrowserNavigate", browser, function( link )
        if not link:find("www.convertmp3.io") then
		local vehicle = getPedOccupiedVehicle ( localPlayer )
		local x, y, z = getElementPosition(vehicle)
            currentSound[localPlayer] = playSound3D( link, x, y, z )
			attachElements(currentSound[localPlayer],vehicle)
			setSoundMaxDistance(currentSound[localPlayer],30)
			setSoundVolume(currentSound[localPlayer],50)
        end
    end 
)

Server side: 

addEvent( 'play' , true )

addEventHandler( 'play' , root , 
    function( link )
        triggerClientEvent( root , 'Play' , client , link )
    end
)

 

Link to comment
requestBrowserDomains({"www.convertmp3.io"})
local browser = createBrowser( 1, 1, false )
local currentSound = {}

addEvent( 'Play' , true )
addEventHandler( 'Play' , root ,
    function( link )
        local vehicle = getPedOccupiedVehicle ( source )
        local x, y, z = getElementPosition(vehicle)
        currentSound[source] = playSound3D( link, x, y, z )
        attachElements(currentSound[source],vehicle)
        setSoundMaxDistance(currentSound[source],30)
        setSoundVolume(currentSound[source],50)
    end
)

function fetch(_,url)
    if url and url ~= "" then
        fetchRemote("http://www.convertmp3.io/fetch/?format=JSON&video="..url, callback)
    end
end
addCommandHandler("p",fetch)

function callback(data, error)
    if (error ~= 0) then
        return outputChatBox(error)
    end
    if (data == "ERROR") then
        return outputChatBox("data error")
    end
    local data = fromJSON("["..data.."]")
    if (data) then
        outputChatBox("Title: "..data.title)
        outputChatBox("Length: "..data.length)
        outputChatBox("Link: "..data.link)
        loadBrowserURL( browser, data.link  )
    end
end

addEventHandler( "onClientBrowserNavigate", browser, function( link )
        if not link:find("www.convertmp3.io") then
            triggerServerEvent( 'play' , localPlayer , link ) -- trigger the event when the script actially gets the playable link!
        end
    end 
)

 

  • Thanks 1
Link to comment
6 minutes ago, Mr.Loki said:

requestBrowserDomains({"www.convertmp3.io"})
local browser = createBrowser( 1, 1, false )
local currentSound = {}

addEvent( 'Play' , true )
addEventHandler( 'Play' , root ,
    function( link )
        local vehicle = getPedOccupiedVehicle ( source )
        local x, y, z = getElementPosition(vehicle)
        currentSound[source] = playSound3D( link, x, y, z )
        attachElements(currentSound[source],vehicle)
        setSoundMaxDistance(currentSound[source],30)
        setSoundVolume(currentSound[source],50)
    end
)

function fetch(_,url)
    if url and url ~= "" then
        fetchRemote("http://www.convertmp3.io/fetch/?format=JSON&video="..url, callback)
    end
end
addCommandHandler("p",fetch)

function callback(data, error)
    if (error ~= 0) then
        return outputChatBox(error)
    end
    if (data == "ERROR") then
        return outputChatBox("data error")
    end
    local data = fromJSON("["..data.."]")
    if (data) then
        outputChatBox("Title: "..data.title)
        outputChatBox("Length: "..data.length)
        outputChatBox("Link: "..data.link)
        loadBrowserURL( browser, data.link  )
    end
end

addEventHandler( "onClientBrowserNavigate", browser, function( link )
        if not link:find("www.convertmp3.io") then
            triggerServerEvent( 'play' , localPlayer , link ) -- trigger the event when the script actially gets the playable link!
        end
    end 
)

 

Thank you! 

Link to comment
50 minutes ago, Mr.Loki said:

requestBrowserDomains({"www.convertmp3.io"})
local browser = createBrowser( 1, 1, false )
local currentSound = {}

addEvent( 'Play' , true )
addEventHandler( 'Play' , root ,
    function( link )
        local vehicle = getPedOccupiedVehicle ( source )
        local x, y, z = getElementPosition(vehicle)
        currentSound[source] = playSound3D( link, x, y, z )
        attachElements(currentSound[source],vehicle)
        setSoundMaxDistance(currentSound[source],30)
        setSoundVolume(currentSound[source],50)
    end
)

function fetch(_,url)
    if url and url ~= "" then
        fetchRemote("http://www.convertmp3.io/fetch/?format=JSON&video="..url, callback)
    end
end
addCommandHandler("p",fetch)

function callback(data, error)
    if (error ~= 0) then
        return outputChatBox(error)
    end
    if (data == "ERROR") then
        return outputChatBox("data error")
    end
    local data = fromJSON("["..data.."]")
    if (data) then
        outputChatBox("Title: "..data.title)
        outputChatBox("Length: "..data.length)
        outputChatBox("Link: "..data.link)
        loadBrowserURL( browser, data.link  )
    end
end

addEventHandler( "onClientBrowserNavigate", browser, function( link )
        if not link:find("www.convertmp3.io") then
            triggerServerEvent( 'play' , localPlayer , link ) -- trigger the event when the script actially gets the playable link!
        end
    end 
)

 

But why not remove the music, when player quit?

Client:

addEvent( 'Stop' , true )
addEventHandler( 'Stop' , root ,
    function()
    local vehicle = getPedOccupiedVehicle ( source )
	destroyElement(currentSound[source])
	detachElements ( currentSound[source], vehicle )
    end
)

Server:

function quitPlayer ()
        triggerClientEvent( root , 'Stop' , client )
end
addEventHandler ( "onPlayerQuit", root, quitPlayer )

 

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