mwg API
    Preparing search index...

    Class MusicPlayer

    One music track at a time with the controls a game's jukebox needs: load a decoded track or a rendered MIDI file, play, pause, resume, read the position, seek, and be told when the track ends so the game can pick the next one. It wraps a Channel, so the buffer, the loop region and the playhead behave as they do there; what it adds is the paused state and the end of the track. Like Music, nothing advances on its own: call update() each frame.

    import { MusicPlayer, renderMidiToBufferAsync } from '@datamoc/mwg/audio';

    declare const context: AudioContext;
    declare const midiBytes: ArrayBuffer;
    declare function nextTrack(): Promise<ArrayBuffer>;

    const player = new MusicPlayer(context, { volume: 0.8 });
    player.load(await renderMidiToBufferAsync(midiBytes));
    player.play();
    async function playNext(): Promise<void> {
    player.load(await renderMidiToBufferAsync(await nextTrack()));
    player.play();
    }
    player.onEnded.add(() => { void playNext(); });

    // in the scene's update:
    player.update();
    Index
    onEnded: Signal<void> = ...

    fires once when a track that was not repeating plays through to its end

    • moves the playhead, keeping a paused track paused

      Parameters

      • pos: number

      Returns void

    • call each frame: fires onEnded once when a non-repeating track has finished

      Returns void