[Release] xSound audio library for FiveM

Link to git: GitHub - Xogy/xsound: Improved audio library for FiveM

#Improved audio library for FiveM

Can work with api interact sound

Just make sure you take all sounds from interact

sound and move them to xsound/html/sounds

Thanks to

for awesome api

GitHub - plunkettscott/interact-sound: A resource providing the ability to play sounds using the FiveM NUI environment.

SoundSystem functions

1. Functions (client side)


Playing sound


  • PlayUrl(name, url, volume, loop, options)
    Will play sound from url (can be heared everywhere)
    argument loop and options are optional, doesn’t have to be used.

  • PlayUrlPos(name, url, volume, Vector3 vec, loop, options)
    Will play sound from url at x,y,z location
    argument loop and options are optional, doesn’t have to be used.

options list

  • onPlayStart
  • onPlayEnd
  • onLoading
  • onPlayPause
  • onPlayResume

Manipulation with sound


  • Position(name, Vector3 vec)
    Will update location of sound

  • Distance(name, newDistance)
    Will set new playing distance from location

  • Destroy(name)
    Will destroy sound

  • Pause(name)
    Will pause sound

  • Resume(name)
    Will resume sound

  • setVolume(name,volume) volume is from 0.0 to 1.0
    Will set a new value to volume. Should be used for non 3D sound

  • setVolumeMax(name,volume) volume is from 0.0 to 1.0
    will set new value to max volume. Should be used only for 3D sound

  • setTimeStamp(name, time) will set a new timestamp.


Events (client side only)


  • onPlayStart(name, function)

    This event will trigger after the sound

    is loaded and start playing in game.

  • onPlayEnd(name, function)

    This event will be triggered after sound end.

  • onLoading(name, function)

    This event will be triggered when the sound start loading.

  • onPlayPause(name, function)

    This event will be triggered whenever you pause sound.

  • onPlayResume(name, function)

    This event will be triggered whenever you resume sound.


Getting info about sound


  • soundExists(name)
    Will return true/false if sound exists

  • isPaused(name)
    Will return true/false if song is paused

  • isPlaying(name)
    Will return true/false if song is playing

  • isLooped(name)
    Will return true/false if sound is looped

  • getDistance(name)
    Will return distance in Integer

  • getVolume(name)
    Will return current volume of music.

  • getPosition(name)
    Will return vector3

  • isDynamic(name)
    Will return if sound is 3D or 2D (3D = true, 2D = false)

  • getTimeStamp(name), – returns current timestamp

  • getMaxDuration(name), – returns max duration of sound

  • getLink(name)
    Will return url link

  • getInfo(name)
    Will return an array with info of song…
    it will return

{
volume,      -- value from 0.0 to 1.0
url ,        -- sound url
id,          -- id 
position,    -- will be nil if position isnt set.
distance,    -- distance in integer
playing,     -- true/false
paused,      -- true/false
loop,        -- true/false
isDynamic,   -- true/false
timeStamp,   -- returns current timestamp
maxDuration, -- returns max duration of sound
}

1. Functions (Server side)


Playing sound


  • PlayUrl(source, name, url, volume)
    Will play sound from url (can be heared everywhere)

  • PlayUrlPos(source, name, url, volume, Vector3 vec)
    Will play sound from url at x,y,z location


Manipulation with sound


  • -1 for source work aswell

  • Position(source, name, Vector3 vec)
    Will update location of sound

  • Distance(source, name, newDistance)
    Will set new playing distance from location

  • Destroy(source, name)
    Will destroy sound

  • Pause(source, name)
    Will pause sound

  • Resume(source, name)
    Will resume sound

  • setVolume(source, name,volume) volume is from 0.0 to 1.0
    Will set a new value to volume. Should be used for non 3D sound

  • setVolumeMax(source, name,volume) volume is from 0.0 to 1.0
    will set new value to max volume. Should be used only for 3D sound

  • setTimeStamp(source ,name, time) will set a new timestamp.

    TIMESTAMP is in a seconds only !


##Example client

example how to use options.

xSound = exports.xsound
Citizen.CreateThread(function()
    local pos = GetEntityCoords(PlayerPedId())
    local options =
    {
        onPlayStart = function(event) -- event argument returns getInfo(id)
            print("oh yeah! PARTY!")
        end,
        onPlayEnd = function(event) 
            print("oh... already end ? :( Song name ? pls")
            print(event.url)
        end,
    }   

    xSound:PlayUrlPos("name","http://relisoft.cz/assets/brainleft.mp3",1,pos, false, options)
end)   

example how to use new events.

xSound = exports.xsound
Citizen.CreateThread(function()
   local pos = GetEntityCoords(PlayerPedId())
   local id = "name"
   xSound:PlayUrlPos(id ,"http://relisoft.cz/assets/brainleft.mp3",1,pos)

   xSound:onPlayStart(id, function(event) -- event argument returns getInfo(id) 
       print("oh yeah! PARTY!")
   end)

   xSound:onPlayEnd(id, function(event)  
       print("oh... already end ? :( Song name ? pls")
       print(event.url)
   end)
end)   

How to play from direct URL

xSound = exports.xsound
Citizen.CreateThread(function()
    local pos = GetEntityCoords(PlayerPedId())
    xSound:PlayUrlPos("name","http://relisoft.cz/assets/brainleft.mp3",1,pos)
    xSound:Distance("name",100)
    
    Citizen.Wait(1000*30)
    xSound:Destroy("name")
end)

How to play youtube link

xSound = exports.xsound
Citizen.CreateThread(function()
    local pos = GetEntityCoords(PlayerPedId())
    xSound:PlayUrlPos("name","https://www.youtube.com/watch?v=6Dh-RL__uN4",1,pos)
    --some links will not work cause to copyright or autor did not allowed to play video from iframe.
    xSound:Distance("name",100)
    
    Citizen.Wait(1000*30)
    xSound:Destroy("name")
end)

How to be followed by the sound
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)

Showcase how it can stream sound at game

26 Likes

Neat :slight_smile:

1 Like

@Xogos1 please share the radio

how staaart

@Lucik1 you have to type “ensure xsound” Lul it is an api… you have to build your own script around it.

@MY_LIFE_RP nope…

no… how find the radio… where is it… sorry for bad english. :slight_smile:

@Lucik1 it is just a showcase what my api can do and how it can be used. You can’t get the radio anywhere… i will remove the showcase for radio and dj resource to avoid confusion.

so… is not posible to find radio?..

ive been trying but havent been successful. How would i be able to make a notification that would bring up the song name?

Hello, depends on what link you’re using.

Direct url:

function editString(string){
    var str = string;
    var res = str.split("/");
    var final = res[res.length - 1];
    final = final.replace(".mp3", " ");
    final = final.replace(".wav", " ");
    final = final.replace(".wma", " ");
    final = final.replace(".wmv", " ");

    final = final.replace(".aac", " ");
    final = final.replace(".ac3", " ");
    final = final.replace(".aif", " ");
    final = final.replace(".ogg", " ");
    final = final.replace("%20", " ");

    return final;
}

it is not always 100% accurate, but it will get its job done.

From youtube

html

<script src="https://s.ytimg.com/yts/jsbin/www-widgetapi-vflJJaNgk/www-widgetapi.js"></script>
<script src="https://www.youtube.com/iframe_api"></script>

js

function getYoutubeUrlId(url)
{
    var videoId = "";
    if( url.indexOf("youtube") !== -1 ){
        var urlParts = url.split("?v=");
        videoId = urlParts[1].substring(0,11);
    }

    if( url.indexOf("youtu.be") !== -1 ){
        var urlParts = url.replace("//", "").split("/");
        videoId = urlParts[1].substring(0,11);
    }
    return videoId;
}

function getName(name){
     $.post('http://yourresource/name', JSON.stringify({
            song_name: name,
        }));
}

var yPlayer = new YT.Player("DIV NAME", {
        height: '0',
        width: '0',
        videoId: getYoutubeUrlId("link to youtube"),
        events: {
                'onReady': function(event){
                getName(event.target.getVideoData().title);
        },
        }
});

this method is 100% accurate. just dont forget to destroy it after you’re done…

you can destroy it with function

yPlayer.stopVideo();
yPlayer.destroy();

you need to first stop player, so the events such like a “onReady” wont tell you warning like “dom is not attached”

Is there any way to make this server sided so everyone hears the same thing and anyone that comes in hears the same. This is perfect for all the clubs

my api isnt server sided, but you can cache sound that is playing in club and play it on connect.

just make event listener and call it for everyone when you play it.

Nice good job

1 Like

Good job men, i make this using your script!

3 Likes

Can you share your booxbox resource

Will you share this? looks awesome.
BTW: Pibes chorros…epic song xD.

1 Like

Very nice release but can you implement 3d audio ?

can you explain further ? This library already simulating “3D”

i mean, this simulates 3d by increasing/decreasing volume, im talking about 7.1 where you can identify the source…

seems like javascript neither html supports 7.1 sound, however javascript can support or simulate 5.1 sound… i dont have enough knowledge in javascript to do it so…