Sound, music and video
Example: Effects Lab has a sound board, a music player and a video screen, and Crater Canyon uses ambient loops and music.
Sound effects
With the audio permission:
audio.play("sounds/click.wav") -- heard wherever you are
local engine = audio.play("sounds/engine.ogg", { position = block.position, loop = true, volume = 0.6, pitch = 1.2 })
engine.position = block.position -- move it with the thing making it
engine.pitch = 0.8 + speed / 50
engine:fadeOut(1.5)| Option | Description |
|---|---|
position | Play in the world: louder when near, and from a direction. Leave it out for interface sounds. |
minDistance, maxDistance | Where the sound starts to fade and where it can no longer be heard, in metres |
volume | 0–1 |
pitch | Speed, where 1 is normal. Changes the pitch too. |
loop | Repeat until stopped |
fadeIn | Seconds to fade in |
A Sound has volume, pitch and position you can change, ready (true once loaded), time(), isPlaying(), fadeOut(seconds) and stop(). Sounds that don't loop clean themselves up when they finish. audio.stopAll() stops everything your mod plays.
Music
local theme = audio.music("music/theme.ogg", { volume = 0.5, fadeIn = 3 })
theme:fadeOut(2)Music loops by default, is not placed in the world, and plays on the game's music channel.
Formats and the player's volume
| Format | Use for |
|---|---|
.wav (PCM 8, 16, 24 or 32-bit, or float) | Short effects. Ready instantly. Block sounds must be WAV. |
.ogg (Vorbis) | Music and long ambience. Loads in the background, so ready becomes true a moment later. |
.mp3 | The same as .ogg |
All mod audio goes through the game's mixer: the player's effects and music volume sliders apply to your mod, and a mod cannot play louder than the player allows. Each mod may have 128 MB of decoded audio loaded.
Video
With the video permission. The formats are .mp4 (H.264) and .webm (VP8).
local screen = panel:addNew("VisualElement", { style = { width = "480px", height = "270px" } })
local intro = video.onElement("video/intro.mp4", screen, { loop = true })
local tv = world.shape("quad", { position = vec3(0, 2, 6), scale = vec3(3.2, 1.8, 1) })
local clip = video.onObject("video/news.webm", tv, { muted = true })
intro.volume = 0.5
intro:pause(); intro:resume()
intro:onEnd(function() intro:stop() end)
log.info(intro.ready, intro.length, intro.time)Options: loop, muted, width and height (the picture resolution, default 1280×720). A mod plays at most four videos at once. :stop() frees one.
Tip Keep videos short and small: a 1280×720 H.264 at 1.5 Mbit/s is plenty for an in-game screen. Players joining a server download your mod, and the 128 MB download limit applies to each video.