Sound API
The Sound API provides functions to play sounds in the Minecraft game.
All functions in this API are accessible through the sound table.
Playing Sounds
playSound
sound.playSound(soundName, volume, pitch)
Plays a sound at the player's location.
Parameters:
soundName(string) - The name of the sound to play (format: "namespace:sound_id")volume(number) [optional] - The volume of the sound (default: 1.0)pitch(number) [optional] - The pitch of the sound (default: 1.0)
Example:
-- Play a block breaking sound
sound.playSound("minecraft:block.stone.break")
-- Play a note block sound at higher pitch and volume
sound.playSound("minecraft:block.note_block.harp", 2.0, 1.5)
playSoundAt
sound.playSoundAt(soundName, x, y, z, volume, pitch)
Plays a sound at the specified location.
Parameters:
soundName(string) - The name of the sound to play (format: "namespace:sound_id")x(number) - The x coordinatey(number) - The y coordinatez(number) - The z coordinatevolume(number) [optional] - The volume of the sound (default: 1.0)pitch(number) [optional] - The pitch of the sound (default: 1.0)
Example:
-- Play an explosion sound at coordinates (0, 64, 0)
sound.playSoundAt("minecraft:entity.generic.explode", 0, 64, 0)
-- Play a chest opening sound at a specific location with custom volume and pitch
local pos = mc.getPlayerPosition()
sound.playSoundAt("minecraft:block.chest.open", pos.x + 3, pos.y, pos.z, 0.5, 0.8)
Common Minecraft Sounds
Here are some commonly used Minecraft sounds that you can use with the Sound API:
Block Sounds
minecraft:block.stone.break- Stone breakingminecraft:block.wood.break- Wood breakingminecraft:block.grass.break- Grass breakingminecraft:block.glass.break- Glass breakingminecraft:block.anvil.land- Anvil landingminecraft:block.chest.open- Chest openingminecraft:block.chest.close- Chest closingminecraft:block.ender_chest.open- Ender chest openingminecraft:block.piston.extend- Piston extendingminecraft:block.piston.contract- Piston contracting
Entity Sounds
minecraft:entity.player.attack.strong- Strong attackminecraft:entity.player.attack.weak- Weak attackminecraft:entity.player.hurt- Player hurtminecraft:entity.player.levelup- Level upminecraft:entity.experience_orb.pickup- XP orb pickupminecraft:entity.item.pickup- Item pickupminecraft:entity.generic.explode- Explosionminecraft:entity.lightning_bolt.thunder- Thunderminecraft:entity.arrow.hit- Arrow hitminecraft:entity.enderman.teleport- Enderman teleport
UI Sounds
minecraft:ui.button.click- Button clickminecraft:ui.toast.in- Toast notification inminecraft:ui.toast.out- Toast notification out
Note Block Sounds
minecraft:block.note_block.harp- Harp noteminecraft:block.note_block.bass- Bass noteminecraft:block.note_block.snare- Snare noteminecraft:block.note_block.hat- Hat noteminecraft:block.note_block.bell- Bell noteminecraft:block.note_block.chime- Chime noteminecraft:block.note_block.guitar- Guitar noteminecraft:block.note_block.xylophone- Xylophone noteminecraft:block.note_block.iron_xylophone- Iron xylophone noteminecraft:block.note_block.cow_bell- Cow bell noteminecraft:block.note_block.didgeridoo- Didgeridoo noteminecraft:block.note_block.bit- Bit noteminecraft:block.note_block.banjo- Banjo noteminecraft:block.note_block.pling- Pling note
Usage Examples
Directional Sound Radar
function soundRadar()
-- Get nearby players
local players = player.getPlayersInRange(20)
local playerPos = mc.getPlayerPosition()
for _, p in ipairs(players) do
local entityPos = entity.getEntityPosition(p)
-- Calculate distance
local distance = math.distance(
playerPos.x, playerPos.y, playerPos.z,
entityPos.x, entityPos.y, entityPos.z
)
-- Play sound at player's position with volume based on distance
local volume = 1.0 - (distance / 20)
if volume > 0 then
sound.playSoundAt("minecraft:block.note_block.pling",
entityPos.x, entityPos.y, entityPos.z,
volume, 1.0)
end
end
end
Musical Sequence
function playMelody()
local notes = {
{ sound = "minecraft:block.note_block.harp", pitch = 0.5 },
{ sound = "minecraft:block.note_block.harp", pitch = 0.6 },
{ sound = "minecraft:block.note_block.harp", pitch = 0.7 },
{ sound = "minecraft:block.note_block.harp", pitch = 0.8 },
{ sound = "minecraft:block.note_block.harp", pitch = 0.9 },
{ sound = "minecraft:block.note_block.harp", pitch = 1.0 },
{ sound = "minecraft:block.note_block.harp", pitch = 1.1 },
{ sound = "minecraft:block.note_block.harp", pitch = 1.2 }
}
for _, note in ipairs(notes) do
sound.playSound(note.sound, 1.0, note.pitch)
util.sleep(200) -- 200ms delay between notes
end
end
Ambient Sound Environment
function createAmbience()
-- Play ambient sounds at random intervals
while true do
-- Choose a random ambient sound
local sounds = {
"minecraft:ambient.cave",
"minecraft:ambient.underwater.loop",
"minecraft:ambient.underwater.loop.additions",
"minecraft:ambient.underwater.loop.additions.rare",
"minecraft:ambient.underwater.loop.additions.ultra_rare",
"minecraft:ambient.underwater.enter"
}
local randomSound = sounds[util.randomInt(1, #sounds)]
-- Play at a random position near the player
local pos = mc.getPlayerPosition()
local offsetX = util.randomDouble(-10, 10)
local offsetY = util.randomDouble(-5, 5)
local offsetZ = util.randomDouble(-10, 10)
sound.playSoundAt(randomSound,
pos.x + offsetX,
pos.y + offsetY,
pos.z + offsetZ,
0.3, 1.0)
-- Wait for a random time before playing the next sound
local delay = util.randomInt(5000, 15000)
util.sleep(delay)
end
end