How to play sound in lua

18,018

Solution 1

Lua does not have any built-in sound APIs. As you are on Windows, you have these options:

  • Write bindings for a sound system of choice (OpenAL or something more high-level — as a separate non-Lua-specific question if you need help picking one).

  • Use LuaJIT2 FFI to directly access DLL functions of the sound system of choice.

  • Otherwise, if you do not use LuaJIT2, use Alien FFI library.

  • Or, indeed, use LuaCOM to play some sound (with DirectSound?), if you're familiar with COM stuff.

Solution 2

Lua doesn't have any native sound APIs. If there is a plugin/extension for Lua to accomplish audio control/playback, you'd have to go dig that up seperately. Or if Lua supports COM, you could speak to the Windows audio facilities directly.

Solution 3

You can use the Corona library:

--Loads the selected audio (make sure the name is the same as the .wav file)
local audio1 = audio.loadSound( "audio1.wav" )

--Plays the audio on any available channel (theres up to 32 channels)
local audio1Channel = audio.play( audio1 )

source: http://docs.coronalabs.com/api/library/audio/play.html

Solution 4

proteaAudio is a pretty easy library for lua to play audio or even generate audio on the fly.

Share:
18,018
Mikoláš Štrajt
Author by

Mikoláš Štrajt

Updated on June 04, 2022

Comments

  • Mikoláš Štrajt
    Mikoláš Štrajt almost 2 years

    How to play sound in lua? Is there any simple trick how to do it?

    My goal is to play some ogg (or mp3) files during playing my game, which is programmed in lua. It's running on windows.