Skip to main content

Play, pause, stop

Every practice tool starts with the same three-button transport. play(), pause(), stop(), and playPause() are the API surface behind them โ€” wire a play button on a lesson page to playPause(), bind the spacebar, or step through the score from a keyboard shortcut and alphaTab handles the state machine underneath.

00:00 / 00:00

The transport methodsโ€‹

  • play() โ€” start playback from the current tick. Returns true if playback actually started; false if the player is not yet ready or is already playing.
  • pause() โ€” stop the audio but keep the current tick. Calling play() again resumes from the same position.
  • stop() โ€” halt playback and reset the tick position to the start of the score (or the start of playbackRange if one is set).
  • playPause() โ€” toggle between the two states. Bind this to a single button or the spacebar.

Reacting to state changesโ€‹

The playerStateChanged event fires every time the synth transitions between PlayerState.Paused and PlayerState.Playing. Use it to swap the play icon for a pause icon, disable a seek control, or persist the transport state to your store.

stop() also fires the event with stopped: true so you can distinguish an explicit stop from an ordinary pause.

Show it in codeโ€‹

import * as alphaTab from '@coderline/alphatab';

const container = getAlphaTabContainer();
const api = new alphaTab.AlphaTabApi(container, {
core: { file: '/scores/song.gp' },
player: { playerMode: alphaTab.PlayerMode.EnabledSynthesizer },
});

const button = document.getElementById('play-pause');
button.addEventListener('click', () => api.playPause());

api.playerStateChanged.on((args) => {
const playing = args.state === alphaTab.synth.PlayerState.Playing;
button.textContent = playing ? 'Pause' : 'Play';
});

getAlphaTabContainer() is a placeholder for the platform-specific step of obtaining the render target โ€” a <div> on Web, an AlphaTab control on .NET, or an AlphaTabView on Android. Every other line runs unchanged on all three runtimes.

Where to look nextโ€‹