media.playSound()

Type Function
Library media.*
Return value none
Revision 2017.3060
Keywords media, play, sound, audio
See also media.getSoundVolume()
media.setsoundVolume()
media.pauseSound()
media.stopSound()

Overview

Plays an extended sound (as opposed to an "event sound" which is typically 1-3 seconds in duration), or resumes play of a paused extended sound. You can only have one such sound file open at a time.

You can optionally specify a callback function to be called whenever playback has finished.

Gotchas

Syntax

media.playSound( soundfile [, baseDir] [, onComplete] )

media.playSound( soundfile [, baseDir] [, loop] )
soundfile (required)

String. Name of the sound file. The file must be in a format that is supported by the platform.

onComplete (optional)

Function. If specified, this argument is a completion listener (callback function) that will be called each time the sound finishes playing.

loop (optional)

Boolean. If true, the sound will be played repeatedly. The default is false.

baseDir (optional)

Constant. If specified, this argument is the directory where the sound file is located (if you specified a filename for sound). The default is system.ResourceDirectory. Other options include:

  • system.DocumentsDirectory
  • system.ApplicationSupportDirectory
  • system.TemporaryDirectory
  • system.CachesDirectory

Example

media.playSound( "song.mp3" )
local stopAfter10Seconds = function()
    media.stopSound()
end
timer.performWithDelay( 10000, stopAfter10Seconds )