[Release] xSound audio library for FiveM

is there a way to play any url like ones that are copyrighted or dont allow embedding???

Client:

id = nil

xSound = exports.xsound

RegisterNetEvent('DP_Radio:speelYoutube')
AddEventHandler('DP_Radio:speelYoutube', function(url)
	local pedId = GetPlayerPed(-1)
	id = "youtube-"..pedId

	if IsPedInAnyVehicle(pedId) then
		local vehId = GetVehiclePedIsIn(pedId, false)
		xSound:PlayUrlPos(id ,"https://www.youtube.com/watch?v=" .. url, 1, GetEntityCoords(vehId))
		xSound:onPlayStart(id, function()
			CreateThread(function()
				while true do
					Wait(200)
					xSound:Position(id, GetEntityCoords(vehId))
				end
			end)
		end)
	else
		xSound:PlayUrlPos(id ,"https://www.youtube.com/watch?v=" .. url, 1, GetEntityCoords(pedId))
		xSound:onPlayStart(id, function()
			CreateThread(function()
				while true do
					Wait(200)
					xSound:Position(id, GetEntityCoords(pedId))
				end
			end)
		end)
	end
end)

RegisterNetEvent('DP_Radio:speelMP3')
AddEventHandler('DP_Radio:speelMP3', function(url)
	local pedId = GetPlayerPed(-1)
	id = "MP3-"..pedId
	
	if IsPedInAnyVehicle(pedId) then
		local vehId = GetVehiclePedIsIn(pedId, false)
		xSound:PlayUrlPos(id , url, 1, GetEntityCoords(vehId))
		xSound:onPlayStart(id, function()
			CreateThread(function()
				while true do
					Wait(200)
					xSound:Position(id, GetEntityCoords(vehId))
				end
			end)
		end)
	else
		xSound:PlayUrlPos(id , url, 1, GetEntityCoords(pedId))
		xSound:onPlayStart(id, function()
			CreateThread(function()
				while true do
					Wait(200)
					xSound:Position(id, GetEntityCoords(pedId))
				end
			end)
		end)
	end
end)

RegisterNetEvent('DP_Radio:volume')
AddEventHandler('DP_Radio:volume', function(volume)
	if xSound.soundExists(id) then
		xSound:setVolume(id, volume/100)
		print('Volume ingesteld op: '..volume)
	end
end)

RegisterNetEvent('DP_Radio:stop')
AddEventHandler('DP_Radio:stop', function()
	if xSound.soundExists(id) then
		xSound:Destroy(id)
		print('Radio gestopt '..id)
	end
end)

Server:


RegisterCommand('youtube', function(source, args, rawCommand)
	local _source = source
	TriggerClientEvent('DP_Radio:speelYoutube', _source, args[1])
end)

RegisterCommand('yt', function(source, args, rawCommand)
	local _source = source
	TriggerClientEvent('DP_Radio:speelYoutube', _source, args[1])
end)

RegisterCommand('mp3', function(source, args, rawCommand)
	local _source = source
	if string.match(rawCommand, 'mp3 ') then
		local url = string.sub(rawCommand, 4)
		TriggerClientEvent('DP_Radio:speelMP3', _source, url)
	end
end)

RegisterCommand('mp4', function(source, args, rawCommand)
	local _source = source
	if string.match(rawCommand, 'mp4 ') then
		local url = string.sub(rawCommand, 4)
		TriggerClientEvent('DP_Radio:speelMP3', _source, url)
	end
end)

RegisterCommand('stopradio', function(source, args, rawCommand)
	local _source = source
	TriggerClientEvent('DP_Radio:stop', _source)
end)

RegisterCommand('volume', function(source, args, rawCommand)
	local _source = source
	local volume = tonumber(args[1])
	if volume < 0 or volume > 100 then
		print('Volume minimaal 1 en maximaal 100!')
	else
		TriggerClientEvent('DP_Radio:volume', _source, volume)
	end
end)

This is working, some kind, but does anybody know how to stop the radio?
If i use xSound:soundExists than the result is always false, but if i do not use this check the radio will stop, but i get error in console (xsound/client/export/manipuilation:20 = nil)

Hello, am I the only one to not hear sound please ? When I play a music everyone can hear it except me, I tried everything update drivers, change compatibility, run in administrator nothing help to fix my bug :confused:

Any plans on Updating the TextToSpeech api that we can change the api in config or something like that, because google translate api is behind a ā€œpaywallā€ and thats why tts doesnt work any more.

https://translate.google.com/translate_tts?ie=UTF-8&q=test&tl=en-US&total=1&idx=0&client=tw-ob

still works for me. You’re most likely using it just wrong.

Okey, so thats wrong?

local xSound = exports.xsound
.
.
.
xSound:TextToSpeech("name", "en-US", "Test",  1)

Seems you’re correct. For some reason, NUI spits some errors in tts.js from google. I will look at it when I will have more time for it. Thanks for reporting it.

1 Like

Hey, is there anyway to PlayFromURL by using a .ogg file within a resource on server? My Problem is that sounds are time triggered and when loading from URL i have a slight delay

Hey i would like to trigger a sound whit an event.
So when the player trigger the event, everyone around him ear the sound whit a distance…! i dont want put positions. it is possible to trigger on the ped position and stop until the sound is done.

Whats are the possibility to use an export are ever to stop a sound. so if the police enter a code on something the alarm will stop

this is the code i try to use… but not amazing at that… ! thanks

RegisterNetEvent("frazz:vol")
AddEventHandler("frazz:vol", function(type, musicId, data)
xSound = exports.xsound
    local pos = GetEntityCoords(PlayerPedId())
    xSound:PlayUrlPos("name","https://www.youtube.com/watch?v=hFIJaB6kVzk",-1,pos)
    --some links will not work cause to copyright or autor did not allowed to play video from iframe.
    xSound:Distance("name",50)
    
    Citizen.Wait(1000*30)
    xSound:Destroy("name")
end)

What i could do in the trigger or the what ever for make start on de source…

Heya!
Any update on the TextToSpeech?
Thanks in advance :slight_smile:

SO i figure out a little but i cant add a triggerevent … its fuck evrything… is just working clientside …

xSound = exports.xsound
Citizen.CreateThread(function()
    local pos = GetEntityCoords(PlayerPedId())
    xSound:PlayUrlPos("vol","https://www.youtube.com/watch?v=hFIJaB6kVzk",1,pos)
    xSound:Distance("vol",50)
end)

hey how can i put the music to play form a location on a loop, been searching for 1h and can’t find any thing.
also i’m not very good with cooding still learning on my on, can u give me a help with this?

i want to place the music in a location like in a bar.

Neeed help whit xsound and server side on position! someone can help?

TextToSpeechPos is working?

Hello,
can someone help me?

There comes no sound ingame.

xSound = exports.xsound
Config = {}
local music = nil
local loop = true

Config.pos = {
    Billa = {
        vec3(29.448215484619,-1342.6865234375,29.497016906738),
        vec3(25.500148773193,-1366.7585449219,29.364505767822)
    }
}

Citizen.CreateThread(function()
    while true do
        if loop then
            for i = 1, #Config.pos.Billa, 1 do
                PlayBillaSound(i, Config.pos.Billa[i])
            end
        end
        Citizen.Wait(1000)
    end
end)

function PlayBillaSound(id, pos)
    local random = math.random(1, 3)
    loop = false

    if random == 1 then
        music = "https://www.youtube.com/watch?v=9_qk6q0kyj0"
    elseif random == 2 then
        music = "https://www.youtube.com/watch?v=Sn2cAW8zlKU"
    elseif random == 3 then
        music = "https://www.youtube.com/watch?v=pJqUs6bkWkk"
    end

    print(id)
    print(music)
    print(pos)
    xSound:PlayUrlPos(id , music, 1, pos)
    xSound:Distance(id, 10)

    xSound:onPlayEnd(id, function(event)
        print("Ende")
        loop = true
    end)
end

so i placed my interact_sound sounds file inside the xsound html folder and removed the interact_sounds folder since my gksphone has a built in interact_sounds file inside it and Im not hearing any sounds, no seatbelt, no weather alert, no cuffing, nothing. help?

Hey, just wondering if there is anyway to stop the sound lagging behind when moving fast in a vehicle? thanks

How would you use the mp3 from the html/sounds but without a specific vector3 ?

xSound:PlayUrlPos(ā€œnameā€,ā€œ./sounds/horror_demonic_laugh.mp3ā€,0.2, vector3(2558.6, 6156.64, 162.12))
I’m trying to make it work just when you do an action, I want it to play on my ped and anyone around me and not the entire server.
For ex: I want to put gas in a car, i want the people around me to hear it if they are in the range.

If someone is willing to help i would really appreciate it. I spent like 1 day trying to figure it out and can’t seem to make it work. with the old interact-sound it was playing on the entire server and i don’t want such thing.
Thank you all and hope somoone will help !
Cheers !!!

Hi, friend.
you can try this:

local PlayerPed = PlayerPedId()
local coords = GetEntityCoords(PlayerPed)
xSound:PlayUrlPos(ā€œnameā€, ā€œ./sounds/horror_demonic_laugh.mp3ā€, 0.2, coords)

Since if you’re getting gas, your vehicle won’t move, you shouldn’t update the position.
I also guess you’ll have to destroy the sound, when you’re done.

xSound:Destroy(ā€œnameā€)

Thanks, i’ll try.