← Back to Docs
  • index.html
  • Clayground
  • Clayground Sound Module
  • Clayground 2025.2
  • Clayground Sound Module

    The Clayground.Sound module provides cross-platform audio playback for games:

    Platform Support

    PlatformBackend
    WASM (Browser)Web Audio API via Emscripten
    Desktop (Windows/macOS/Linux)Qt Multimedia (QSoundEffect, QMediaPlayer)
    Mobile (iOS/Android)Qt Multimedia

    Usage

    Import the module:

    import Clayground.Sound

    Sound Effects

    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()

    Background Music

    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()

    Lazy Loading

    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
    }

    Technical Notes

    See also Sound and Music.

    Music

    Play background music with play/pause/stop/loop controls

    Sound

    Play sound effects with support for overlapping playback