Skip to content

Events Reference

This page documents event-name constants, payload maps, and event listener helpers exported by @ddgutierrezc/legato-capacitor.

Player lifecycle event names.

import { AUDIO_PLAYER_EVENTS } from '@ddgutierrezc/legato-capacitor';
// AUDIO_PLAYER_EVENTS = [
// 'playback-state-changed',
// 'playback-active-track-changed',
// 'playback-queue-changed',
// 'playback-progress',
// 'playback-ended',
// 'playback-error'
// ];

Remote control event names.

import { MEDIA_SESSION_EVENTS } from '@ddgutierrezc/legato-capacitor';
// MEDIA_SESSION_EVENTS = [
// 'remote-play',
// 'remote-pause',
// 'remote-next',
// 'remote-previous',
// 'remote-seek'
// ];

All Legato event names (union of player and media session events).

import { LEGATO_EVENTS } from '@ddgutierrezc/legato-capacitor';
// LEGATO_EVENTS = [
// 'playback-state-changed',
// 'playback-active-track-changed',
// 'playback-queue-changed',
// 'playback-progress',
// 'playback-ended',
// 'playback-error',
// 'remote-play',
// 'remote-pause',
// 'remote-next',
// 'remote-previous',
// 'remote-seek'
// ];

addAudioPlayerListener(eventName, listener)

Section titled “addAudioPlayerListener(eventName, listener)”

Registers a listener for player lifecycle events.

import { addAudioPlayerListener } from '@ddgutierrezc/legato-capacitor';
const handle = await addAudioPlayerListener('playback-state-changed', (payload) => {
console.log('State:', payload.state);
});

addMediaSessionListener(eventName, listener)

Section titled “addMediaSessionListener(eventName, listener)”

Registers a listener for media session events.

import { addMediaSessionListener } from '@ddgutierrezc/legato-capacitor';
const handle = await addMediaSessionListener('remote-play', () => {
console.log('User pressed play');
});

Registers a listener for any Legato event.

import { addLegatoListener } from '@ddgutierrezc/legato-capacitor';
const handle = await addLegatoListener('playback-progress', (payload) => {
console.log(`Position: ${payload.position}s / ${payload.duration}s`);
});

These helpers provide a simpler API for common event subscriptions.

HelperEventDescription
onPlaybackStateChangedplayback-state-changedPlayback state transitions.
onPlaybackActiveTrackChangedplayback-active-track-changedActive track or index changes.
onPlaybackQueueChangedplayback-queue-changedQueue structure changes.
onPlaybackProgressplayback-progressPosition/duration/buffer progress.
onPlaybackEndedplayback-endedTerminal playback state.
onPlaybackErrorplayback-errorPlayback error events.
onRemotePlayremote-playRemote play command.
onRemotePauseremote-pauseRemote pause command.
onRemoteNextremote-nextRemote next command.
onRemotePreviousremote-previousRemote previous command.
onRemoteSeekremote-seekRemote seek command.
import {
onPlaybackStateChanged,
onPlaybackProgress,
onPlaybackEnded,
} from '@ddgutierrezc/legato-capacitor';
// Subscribe to state changes
const unsubState = onPlaybackStateChanged((payload) => {
console.log('State changed:', payload.state);
});
// Subscribe to progress
const unsubProgress = onPlaybackProgress((payload) => {
console.log(`Position: ${payload.position.toFixed(1)}s`);
});
// Clean up subscriptions when done
await unsubState.remove();
await unsubProgress.remove();
  • playback-state-changed{ state: PlaybackState }
  • playback-active-track-changed{ track: Track | null; index: number | null }
  • playback-queue-changed{ queue: QueueSnapshot }
  • playback-progress{ position: number; duration: number | null; bufferedPosition: number | null }
  • playback-ended{ snapshot: PlaybackSnapshot }
  • playback-error{ error: LegatoError }
  • remote-play{}
  • remote-pause{}
  • remote-next{}
  • remote-previous{}
  • remote-seek{ position: number }

Union of all Legato event name strings.

type LegatoEventName =
| 'playback-state-changed'
| 'playback-active-track-changed'
| 'playback-queue-changed'
| 'playback-progress'
| 'playback-ended'
| 'playback-error'
| 'remote-play'
| 'remote-pause'
| 'remote-next'
| 'remote-previous'
| 'remote-seek';

Map of event names to their payload types.

interface LegatoEventPayloadMap {
'playback-state-changed': { state: PlaybackState };
'playback-active-track-changed': { track: Track | null; index: number | null };
'playback-queue-changed': { queue: QueueSnapshot };
'playback-progress': { position: number; duration: number | null; bufferedPosition: number | null };
'playback-ended': { snapshot: PlaybackSnapshot };
'playback-error': { error: LegatoError };
'remote-play': {};
'remote-pause': {};
'remote-next': {};
'remote-previous': {};
'remote-seek': { position: number };
}

Listener signature for any Legato event.

type LegatoListener<E extends LegatoEventName> = (
payload: LegatoEventPayloadMap[E]
) => void;