Tokio

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

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?

Share this post


Link to post

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

Share this post


Link to post
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.. 

Share this post


Link to post

 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
)

 

Share this post


Link to post
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

Share this post


Link to post

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

Share this post


Link to post
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
)

 

Share this post


Link to post
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

Share this post


Link to post
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! 

Share this post


Link to post
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 )

 

Share this post


Link to post

client is only predefined when calling a custom event from the client. Change it to source.
Also you are destroying the element before detaching it which will cause an error.

  • Thanks 1

Share this post


Link to post
3 minutes ago, Mr.Loki said:

client is only predefined when calling a custom event from the client. Change it to source.
Also you are destroying the element before detaching it which will cause an error.

thanks :D

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.