[Release] xSound audio library for FiveM

Can we add this to a custom add-on car ? Like for example I have a car with a cool soundsystem, I’d Love to be able to stream some music around it so when car’s on town, it goes around with loud music playing around it, and IF YES, is there a way to randomize the song from a predefined link? or even better, a way to enter the link to a song (like a boombox) and make it play all around the car?
I’d really love this…

I’m also looking to do that, but I’m not getting it and I’m trying this new API, but I think it’s not possible

the update rate has to be constant for the sound to follow, but there would have to be a function for that. If using PlayUrlPos updating it in a loop to get the new positions of the vehicle does not work

My sound system doesnt really handle very well speeding cars and updating positions to it… however it is possible but if you’re going to drive more than 150 km/h with car the audio will get chopier and chopier every time…

Client:

xSound = exports.xsound

local musicId
local playing = false
Citizen.CreateThread(function()
    Citizen.Wait(1000)
    musicId = "music_id_" .. PlayerPedId()
    local pos
    while true do
        Citizen.Wait(100)
        if xSound:soundExists(musicId) and playing then
            if xSound:isPlaying(musicId) then
                pos = GetEntityCoords(PlayerPedId())
                TriggerServerEvent("myevent:soundStatus", "position", musicId, { position = pos })
            else
                Citizen.Wait(1000)
            end
        else
            Citizen.Wait(1000)
        end
    end
end)

RegisterCommand("playmusic", function(source, args, rawCommand)
    local pos = GetEntityCoords(PlayerPedId())
    playing = true
    TriggerServerEvent("myevent:soundStatus", "play", musicId, { position = pos, link = "https://www.youtube.com/watch?v=6Dh-RL__uN4" })
end, false)

RegisterNetEvent("myevent:soundStatus")
AddEventHandler("myevent:soundStatus", function(type, musicId, data)
    if type == "position" then
        if xSound:soundExists(musicId) then
            xSound:Position(musicId, data.position)
        end
    end

    if type == "play" then
        xSound:PlayUrlPos(musicId, data.link, 1, data.position)
        xSound:Distance(musicId, 20)
    end
end)

Server

RegisterNetEvent("myevent:soundStatus")
AddEventHandler("myevent:soundStatus", function(type, musicId, data)
    TriggerClientEvent("myevent:soundStatus", -1, type, musicId, data)
end)

Hope its help… what it does ? it will play music for everyone on server each 100miliseconds it will set a new position to song where it has to be played.

1 Like

with this your code everyone on the server is listening … even though it is very far away

and is cutting the sound a lot in a way that you can’t hear the music

yeah, forgot to check if i am the one who playing sound… also please read what i type next time :slight_smile: "My sound system doesnt really handle very well speeding cars and updating positions to it… however it is possible but if you’re going to drive more than 150 km/h with car the audio will get chopier and chopier every time…"

local musicId
local playing = false
Citizen.CreateThread(function()
    Citizen.Wait(1000)
    musicId = "music_id_" .. PlayerPedId()
    local pos
    while true do
        Citizen.Wait(100)
        if xSound:soundExists(musicId) and playing then
            if xSound:isPlaying(musicId) then
                pos = GetEntityCoords(PlayerPedId())
                TriggerServerEvent("myevent:soundStatus", "position", musicId, { position = pos })
            else
                Citizen.Wait(1000)
            end
        else
            Citizen.Wait(1000)
        end
    end
end)

RegisterCommand("playmusic", function(source, args, rawCommand)
    local pos = GetEntityCoords(PlayerPedId())
    playing = true
    TriggerServerEvent("myevent:soundStatus", "play", musicId, { position = pos, link = "https://www.youtube.com/watch?v=6Dh-RL__uN4" })
end, false)
1 Like

yes but even with the vehicle stopped it is cutting

works just fine for me… you have to fix it somehow then…

test with one more player

i told you it works just fine for me…

with the same code that you sent or made any changes?

will test this later tonight, I assume I can make it randomize a link from a list if I add the randomize function to it and define it somehow, then change link = “https://www.youtube.com/watch?v=6Dh-RL__uN4" to link = definedrandomlink

am i right?

now it works bro thanks a lot for making this music js I was a long time behind something like that thanks a lot

1 Like
local musicId
local playing = false
Citizen.CreateThread(function()
    Citizen.Wait(1000)
    musicId = "music_id_" .. PlayerPedId()
    local pos
    while true do
        Citizen.Wait(100)
        if xSound:soundExists(musicId) and playing then
            if xSound:isPlaying(musicId) then
                pos = GetEntityCoords(PlayerPedId())
                TriggerServerEvent("myevent:soundStatus", "position", musicId, { position = pos })
            else
                Citizen.Wait(1000)
            end
        else
            Citizen.Wait(1000)
        end
    end
end)

RegisterCommand("playmusic", function(source, args, rawCommand)
    local pos = GetEntityCoords(PlayerPedId())
    local songs = { "https://www.youtube.com/watch?v=Cub8V9DXApI", "https://www.youtube.com/watch?v=6Dh-RL__uN4", "https://www.youtube.com/watch?v=XbGs_qK2PQA","https://www.youtube.com/watch?v=r_0JjYUe5jo"}
    playing = true
    TriggerServerEvent("myevent:soundStatus", "play", musicId, { position = pos, link = (songs[math.random(#songs)]) })
end, false)

Gonna test like this…so we can randomize a song from a list :stuck_out_tongue:

thank you for the release

2 Likes

Any chance for spotify support? If you link a spotify playlist it’ll open a browser window and just needs to “hit play” just like an embedded youtube video. Tried to dig through your code to add it myself but couldn’t figure out where you were processing if they are youtube links or not.

Other than that, works flawlessly in the script I wrote. Thanks a lot for releasing this!

EDIT: Looked into it a bit more, seems like there won’t be a way to control volume in the browser unless you have a way to manually control it. It doesn’t have a keybind like youtube. Leaving the post in case you wanna give it a shot

1 Like

well yeah, i wont really support spotify… youtube and direct url is enough in my opinion… it can also stream youtube livestreams / radio such like this http://ice.actve.net/fm-evropa2-128

Okay, no biggie. I’m using it to stream internet radio stations as is(181fm), like I said works flawlessly

This is great! I wonder if multiple audios can be played at the same time