Difference between revisions of "PlaySound"

From Multi Theft Auto: Wiki
(Changed misleading parameter description)
(25 intermediate revisions by 16 users not shown)
Line 1: Line 1:
 
__NOTOC__  
 
__NOTOC__  
 
{{Client function}}
 
{{Client function}}
Creates a [[sound]] element and plays it immediately after creation for the local player.<br />
+
Creates a [[sound]] [[element]] and plays it immediately after creation for the local player.<br />
 
<br />
 
<br />
 
'''Note:''' The only supported audio formats are MP3, WAV, OGG, RIFF, MOD, XM, IT, S3M and PLS(e.g. Webstream).
 
'''Note:''' The only supported audio formats are MP3, WAV, OGG, RIFF, MOD, XM, IT, S3M and PLS(e.g. Webstream).
 
+
{{Note|For performance reasons, when using playSound for effects that will be played lots (i.e. weapon fire), it is recommend that you convert your audio file to a one channel (mono) WAV with sample rate of 22050 Hz or less. Also consider adding a limit on how often the effect can be played e.g. once every 50ms.}}
 
==Syntax==  
 
==Syntax==  
<syntaxhighlight lang="lua">element playSound ( string soundPath, [ bool looped = false ] )</syntaxhighlight>  
+
<syntaxhighlight lang="lua">element playSound ( string soundPath, [ bool looped = false, bool throttled = true ] )</syntaxhighlight>  
 
+
{{OOP||[[Sound]]}}
 
===Required Arguments===  
 
===Required Arguments===  
*'''soundPath:''' The [[filepath]] to the sound file you want to play. (Sound file has to be predefined in the [[meta.xml]] file with <file /> tag. And also can use url instead of [[filepath]] )
+
*'''soundPath:''' the [[filepath]] or URL of the sound file you want to play. ('''Note:''' Playing sound files from other resources requires the target resource to be in the running state)
  
 
===Optional Arguments===  
 
===Optional Arguments===  
 
{{OptionalArg}}  
 
{{OptionalArg}}  
*'''looped:''' A [[boolean]] representing whether the sound will be looped. To loop the sound, use ''true''. Loop is not available for streaming sounds, only for sound files.
+
*'''looped:''' a [[boolean]] representing whether the sound will be looped. To loop the sound, use ''true''. Loop is not available for streaming sounds, only for sound files.
 +
{{New feature/item|3.0150|1.5||
 +
*'''throttled:''' a [[boolean]] representing whether the sound will be throttled (i.e. given reduced download bandwidth). To throttle the sound, use ''true''. Sounds will be throttled per default and only for URLs.
 +
}}
  
 
===Returns===
 
===Returns===
Returns a [[sound]] element if the sound was successfully created, ''false'' otherwise.
+
Returns a [[sound]] [[element]] if the sound was successfully created, ''false'' otherwise.
  
 
==Example==
 
==Example==
Line 25: Line 28:
 
end
 
end
  
addEventHandler("onClientPlayerWasted", getLocalPlayer(), wasted) --add the event handler
+
addEventHandler("onClientPlayerWasted", localPlayer, wasted) --add the event handler
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
==See Also==
 
==See Also==
 
{{Client_audio_functions}}
 
{{Client_audio_functions}}
 +
[[HU:playSound]]
 
[[AR:playSound]]
 
[[AR:playSound]]
 
[[DE:playSound]]
 
[[DE:playSound]]
 +
[[RO:playSound]]

Revision as of 00:53, 29 June 2019

Creates a sound element and plays it immediately after creation for the local player.

Note: The only supported audio formats are MP3, WAV, OGG, RIFF, MOD, XM, IT, S3M and PLS(e.g. Webstream).

[[{{{image}}}|link=]] Note: For performance reasons, when using playSound for effects that will be played lots (i.e. weapon fire), it is recommend that you convert your audio file to a one channel (mono) WAV with sample rate of 22050 Hz or less. Also consider adding a limit on how often the effect can be played e.g. once every 50ms.

Syntax

element playSound ( string soundPath, [ bool looped = false, bool throttled = true ] )

OOP Syntax Help! I don't understand this!

Method: Sound(...)

Required Arguments

  • soundPath: the filepath or URL of the sound file you want to play. (Note: Playing sound files from other resources requires the target resource to be in the running state)

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • looped: a boolean representing whether the sound will be looped. To loop the sound, use true. Loop is not available for streaming sounds, only for sound files.
  • throttled: a boolean representing whether the sound will be throttled (i.e. given reduced download bandwidth). To throttle the sound, use true. Sounds will be throttled per default and only for URLs.

Returns

Returns a sound element if the sound was successfully created, false otherwise.

Example

function wasted (killer, weapon, bodypart) 
	local sound = playSound("sounds/wasted.mp3") --Play wasted.mp3 from the sounds folder
	setSoundVolume(sound, 0.5) -- set the sound volume to 50%
end

addEventHandler("onClientPlayerWasted", localPlayer, wasted) --add the event handler

See Also