The Clayground.Sound module provides cross-platform audio playback for games:
| Platform | Backend |
|---|---|
| WASM (Browser) | Web Audio API via Emscripten |
| Desktop (Windows/macOS/Linux) | Qt Multimedia (QSoundEffect, QMediaPlayer) |
| Mobile (iOS/Android) | Qt Multimedia |
Import the module:
import Clayground.Sound
Use Sound for short audio like jumps, clicks, or explosions:
Sound {
id: jumpSound
source: "sounds/jump.wav"
volume: 0.8
}
// Play sound (can overlap multiple times)
onPlayerJumped: jumpSound.play()
Use Music for longer tracks with playback controls:
Music {
id: bgMusic
source: "music/theme.mp3"
volume: 0.5
loop: true
}
onGameStarted: bgMusic.play()
onGamePaused: bgMusic.pause()
onGameOver: bgMusic.stop()
By default, audio is preloaded when source is set. For memory optimization, enable lazy loading to defer loading until first play:
Sound { source: "sounds/rare_event.wav" lazyLoading: true // Only loads when play() is called }