I've been working on some networking that once a player has started this activity we will begin piping in music using sound.PlayURL()
[B][U]Server-Side:[/U][/B]
[B]To Play Song:[/B]
[CODE]
net.Start( "SetupAmbient" )
net.WriteBool( true )
net.WriteSting( "http://media.soundcloud.com/stream/jwxK3NYUGF2a.mp3" ) -- Placeholder Song
net.Send( ply )
[/CODE]
[B]To Fadeout Song:[/B]
[CODE]
net.Start( "SetupAmbient" )
net.WriteBool( false )
net.WriteSting( "http://media.soundcloud.com/stream/jwxK3NYUGF2a.mp3" ) -- Placeholder Song
net.Send( ply )
[/CODE]
[B][U]Client-Side:[/U][/B]
[B]To Initialize The Song:[/B]
[CODE]
net.Receive( "SetupAmbient", function( length )
URL = net.ReadString()
START = net.ReadBool()
sound.PlayURL( URL, "mono", function( audio_obj )
audio_obj:Play()
end )
end )
[/CODE]
However, It's impossible to fadeout the song without creating a new audio object and running sound.PlayURL() again
isn't it net.WriteString? not net.WriteSting
You need to [B]read [/B]the net message arguments in the[B] same order[/B] that you [B]write [/B]them.
Right now you're writing a bool/string but reading a string/bool. Make them consistent.
This isn't a great example, but the idea is to just store the IGModAudioChannel that gets passed when you use sound.PlayURL into a table, allowing you to use it later.
[code]
-- need a table with a persistent scope, doesn't need to be global
-- just don't make it local unless you will never modify the containing file while the server is running
channels = channels or {}
net.Receive( "SetupAmbient", function( length )
local URL = net.ReadString() -- make sure you localize your variables unless you are using them elsewhere in your code
local START = net.ReadBool()
if ( START ) then
sound.PlayURL( URL, "mono", function( audio_obj )
if ( !IsValid( audio_obj ) ) then return end -- bad URL, audio format, etc
-- if you try to play the sound again, but it's already playing, stop it
if ( IsValid( channels[ URL ] ) ) then
channels[ URL ]:Stop()
end
-- store the channel so we can access it later
channels[ URL ] = audio_obj
end )
else
-- timer or hook to decrement audio_obj's volume over time
-- access the channel again using:
-- channels[ URL ]
end
end )[/code]
Just make a timer and change the volume to it approaches 0 over time.
Sorry, you need to Log In to post a reply to this thread.