Garry's Mod Wiki

sound.PlayFile

  sound.PlayFile( string path, string flags, function callback )

Description

Plays a file from GMod directory. You can find a list of all error codes here

For external file/stream playback, see sound.PlayURL.

This fails for looping .wav files in 3D mode.

Issue Tracker: 1752
This fails with unicode file names.

Issue Tracker: 2304

Arguments

1 string path
The path to the file to play.

Unlike other sound functions and structures, the path is relative to garrysmod/ instead of garrysmod/sound/

2 string flags
Flags for the sound. Can be one or more of following, separated by a space (" "):
  • 3d - Makes the sound 3D, so you can set its position
  • mono - Forces the sound to have only one channel
  • noplay - Forces the sound not to play as soon as this function is called
  • noblock - Disables streaming in blocks. It is more resource-intensive, but it is required for IGModAudioChannel:SetTime.

If you don't want to use any of the above, you can just leave it as "".

3 function callback
Callback function that is called as soon as the the stream is loaded.
Function argument(s):
1 IGModAudioChannel channel - The sound channel. Will be nil if an error occurred.
2 number errorID - ID of an error if an error has occurred. Will be nil, otherwise.
3 string errorName - Name of an error if an error has occurred. Will be nil, otherwise.

Example

Plays a piece of music from Half-Life 2.

sound.PlayFile( "sound/music/hl2_song12_long.mp3", "noplay", function( station, errCode, errStr ) if ( IsValid( station ) ) then station:Play() else print( "Error playing sound!", errCode, errStr ) end end )