audio.getDuration()

Type Function
Library audio.*
Return value Number
Revision Release 2024.3703
Keywords audio, duration
See also audio.loadSound()
audio.loadStream()

Overview

This function returns the total time in milliseconds of the audio resource. If the total length cannot be determined, -1 will be returned.

Gotchas

Assuming a duration can actually be determined, the number returned may not be completely accurate. Particularly for sounds loaded with audio.loadStream(), the information may be a estimate or may just be slightly off due to inaccurate metadata. The type of file and encoding you use may have an impact on the accuracy of this value.

Syntax

audio.getDuration( audioHandle )
audioHandle (required)

Table. The audio object returned by audio.loadSound() or audio.loadStream().

Example

local backgroundMusic = audio.loadStream( "backgroundMusic.m4a" )
local totalTime = audio.getDuration( backgroundMusic )

print( totalTime )