• Playing Sound
    7 replies, posted
Hello! Anyone knows how can I play the "ambient/alarms/siren.wav" sound for 10 seconds?
There are a lot of different ways to play sounds depending on how you want them played, one way you could try would be [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/surface/PlaySound]surface.PlaySound[/url], E.G. [CODE] surface.PlaySound( "ambient/alarms/siren.wav" ) [/CODE] ^ Note that this function doesn't makes the sound play in 3D space, just as if you opened it normally If you wanted that to loop for 10 seconds, you could try calling it in a [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/timer/Create]timer.Create[/url], but there's probably a better way
[QUOTE=MPan1;50488143]There are a lot of different ways to play sounds depending on how you want them played, one way you could try would be [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/surface/PlaySound]surface.PlaySound[/url], E.G. [CODE] surface.PlaySound( "ambient/alarms/siren.wav" ) [/CODE] ^ Note that this function doesn't makes the sound play in 3D space, just as if you opened it normally If you wanted that to loop for 10 seconds, you could try calling it in a [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/timer/Create]timer.Create[/url], but there's probably a better way[/QUOTE] I know how to play sound, I want to play for 10 seconds and I don't know how can I create a timer with this.
Actually, I think you'd probably have to use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/sound/PlayFile]sound.PlayFile[/url] to play the sound since that function lets you use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/IGModAudioChannel/EnableLooping]IGModAudioChannel:EnableLooping[/url] (which makes looping simpler) and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/IGModAudioChannel/Stop]IGModAudioChannel:Stop[/url] (which lets you stop the sound after 10 seconds) [editline]10th June 2016[/editline] I'll try to make an example for this, hang on... [editline]10th June 2016[/editline] This seems to work: [CODE] sound.PlayFile( "sound/ambient/alarms/siren.wav", "noblock", function( audiochannel ) if IsValid( audiochannel ) then audiochannel:EnableLooping( true ) -- not really needed since the sound is longer than 10 seconds audiochannel:Play() timer.Simple( 10, function() audiochannel:Stop() end ) -- stop it after 10 seconds end end ) [/CODE] Sorry for posting a full example if you wanted to work it out yourself, I just like trying new stuff
[QUOTE=MPan1;50488258]Actually, I think you'd probably have to use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/sound/PlayFile]sound.PlayFile[/url] to play the sound since that function lets you use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/IGModAudioChannel/EnableLooping]IGModAudioChannel:EnableLooping[/url] (which makes looping simpler) and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/IGModAudioChannel/Stop]IGModAudioChannel:Stop[/url] (which lets you stop the sound after 10 seconds) [editline]10th June 2016[/editline] I'll try to make an example for this, hang on... [editline]10th June 2016[/editline] This seems to work: [CODE] sound.PlayFile( "sound/ambient/alarms/siren.wav", "noblock", function( audiochannel ) if IsValid( audiochannel ) then audiochannel:EnableLooping( true ) -- not really needed since the sound is longer than 10 seconds audiochannel:Play() timer.Simple( 10, function() audiochannel:Stop() end ) -- stop it after 10 seconds end end ) [/CODE] Sorry for posting a full example if you wanted to work it out yourself, I just like trying new stuff[/QUOTE] Doesn't work, have this error: [CODE][ERROR] gamemodes/test/gamemode/init.lua:95: attempt to call field 'PlayFile' (a nil value) 1. unknown - gamemodes/test/gamemode/init.lua:95 Timer Failed! [RoundProcess][@gamemodes/test/gamemode/init.lua (line 70)][/CODE]
You're calling it serverside when sound.PlayFile is a clientside function. Either put the code in cl_init, or network it with the [url=https://wiki.garrysmod.com/page/Net_Library_Usage]net library[/url].
You can also put if CLIENT in your code, that might work too
But only if its shared, don't accidentally write everything in the wrong file.
Sorry, you need to Log In to post a reply to this thread.