Music stops after going too far away.

I’m making music radio for multiplayer.
It works, but when you go too far away the music stops and when you come back the music doesn’t continue.

How to make it synced?






AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )


local PAUSE = 1


local MUSIC = 
{    
    {
        Snd = Sound( "music/song001.mp3" ),
        Dur = 198
    },


    {
        Snd = Sound( "music/song002.mp3" ),
        Dur = 219
    },


    {
        Snd = Sound( "music/song003.mp3" ),
        Dur = 186
    },


    {
        Snd = Sound( "music/song004.mp3" ),
        Dur = 176
    },


    {
        Snd = Sound( "music/song005.mp3" ),
        Dur = 182
    },


    {
        Snd = Sound( "music/song006.mp3" ),
        Dur = 164
    },


    {
        Snd = Sound( "music/song007.mp3" ),
        Dur = 218
    },


    {
        Snd = Sound( "music/song008.mp3" ),
        Dur = 188
    },    
    
    {
        Snd = Sound( "music/song009.mp3" ),
        Dur = 169
    },
    
    {
        Snd = Sound( "music/song010.mp3" ),
        Dur = 193
    },


    {
        Snd = Sound( "music/song011.mp3" ),
        Dur = 201
    },
    
    {
        Snd = Sound( "music/song012.mp3" ),
        Dur = 187
    },
    
    {
        Snd = Sound( "music/song013.mp3" ),
        Dur = 119
    },


    {
        Snd = Sound( "music/song014.mp3" ),
        Dur = 206
    },
    
    {
        Snd = Sound( "music/song015.mp3" ),
        Dur = 187
    },
    
    {
        Snd = Sound( "music/song016.mp3" ),
        Dur = 143
    },


    {
        Snd = Sound( "music/song017.mp3" ),
        Dur = 181
    },


    {
        Snd = Sound( "music/song018.mp3" ),
        Dur = 167
    },
}


-- Do not edit these as it could corrupt this entity!
include('shared.lua')
function ENT:SpawnFunction(ply, tr)
    if (!tr.Hit) then return end
    local SpawnPos = tr.HitPos + tr.HitNormal * 16
    local ent = ents.Create("fallout_eng")
        ent:SetPos(SpawnPos)
    ent:Spawn()
    ent:Activate()
    ent:SetName("Galaxy News Radio")
    ent.Damaged = false;
    self.On = false;
    self.Play = false;


    return ent
end
function ENT:Use(activator, ply)
    if activator:KeyDownLast(IN_USE) then return end 
    if (self.Damaged == true) then return end
    if (not ply:IsPlayer()) then return end
    if (!self.On) then
        local Mus = MUSIC[ math.random( 1, #MUSIC ) ]
        self.Sound = CreateSound(self.Entity, Mus.Snd )
        self.Sound:Play()
        self.On = true;
        self.Play = true;
        self.Duration = CurTime() + PAUSE + Mus.Dur
        self.Musicon = CurTime() + Mus.Dur
        self.Debug = 0
    else
        self.Sound:Stop()
        self.On = false;
    end
end
function ENT:Think()
    if (not self.Damaged == true) and (self.On == true) then
        if CurTime() > self.Musicon then     
                self.Play = false;
                self.Sound:Stop()
                self.Debug = CurTime() + PAUSE
                if (CurTime() > self.Duration) and (self.On == true) then
                    self.Play = true;
                    self.Sound:Stop()
                    local Mus = MUSIC[ math.random( 1, #MUSIC ) ]
                    self.Sound = CreateSound(self.Entity, Mus.Snd )
                    self.Sound:Play()
                    self.Musicon = CurTime() + Mus.Dur
                    self.Duration = CurTime() + PAUSE + Mus.Dur
                end
            if CurTime() > self.Debug and (self.Sound) then
                    self.Sound:ChangePitch(99)
                    self.Sound:ChangePitch(100)
                    self.Debug = CurTime() + PAUSE    
            end
        end
    end
    if (self:WaterLevel() > 0) then
        util.BlastDamage(self.Entity, self.Entity, self.Entity:GetPos(), 1000, 10)
        local effectdata = EffectData()
        effectdata:SetOrigin( self.Entity:GetPos() )
         util.Effect( "Explosion", effectdata, true, true )
        self.Entity:Remove()
    end
    self.Entity:NextThink(1)
end    


function ENT:OnRemove()
    if (self.Sound) then
        self.Sound:Stop()
    end
end
function ENT:Initialize()
    self.On = false
    self.Entity:SetModel("models/fallout3/vintageradio01.mdl")
    self.Entity:SetMaterial("materials/fallout3/vintageradio01.vtf")
    self.Entity:SetMaterial("materials/fallout3/vintageradio01")
    self.Entity:PhysicsInit(SOLID_VPHYSICS)


    self.Entity:SetMoveType(MOVETYPE_VPHYSICS)


    self.Entity:SetSolid(SOLID_VPHYSICS)




    local phys = self.Entity:GetPhysicsObject()


    
if (phys:IsValid()) then


        phys:Wake()


    end


end




Its a problem with CSSoundPatch, check its page on the wiki:

You could try with EmitSound, although it’s not recommended for music, or you could use “self.Sound:Play()” again when the player reenters the hearing range.
Also, you could try playing the music at players position and control the volume yourself, depending on the distance to the entity. It will require a bit more work, but at least the music will never stop. Doing self.Sound:Play() will restart the music file, so it’s not really useful for a radio.

What about using online radio? How I turn the current LUA code to listen from online radio while sound is coming from the entity?