Set Up Legato Capacitor
This guide covers how to install and initialize the Legato Capacitor plugin for audio playback.
Prerequisites
Section titled “Prerequisites”- An existing Capacitor project with at least one native target (
iosand/orandroid) - Node.js 18+ installed
npmor your preferred package manager- Your app already installs Capacitor dependencies and can run
npx cap syncsuccessfully
Installation
Section titled “Installation”-
Install the packages
Terminal window npm install @ddgutierrezc/legato-capacitor @ddgutierrezc/legato-contract -
Sync native platforms
Terminal window npx cap sync -
Verify package installation and sync output
Confirm both packages are present in
package.jsondependencies and thatnpx cap syncfinished without errors.
Initialization
Section titled “Initialization”Import and initialize both audioPlayer and mediaSession:
import { audioPlayer, mediaSession } from '@ddgutierrezc/legato-capacitor';
async function initialize() { await audioPlayer.setup(); await mediaSession.setup();}
initialize();Verify after setup:
const state = await audioPlayer.getState();console.log('Player state after setup:', state);// Expected on first setup: 'idle'Step-by-step verification checklist
Section titled “Step-by-step verification checklist”- After install:
@ddgutierrezc/legato-capacitorand@ddgutierrezc/legato-contractappear in dependencies. - After sync:
npx cap synccompletes without errors. - After setup calls: both
await audioPlayer.setup()andawait mediaSession.setup()resolve. - After state probe:
audioPlayer.getState()resolves (commonly'idle'before queue insertion).
Troubleshooting
Section titled “Troubleshooting”Setup fails before playback commands
Section titled “Setup fails before playback commands”- Verify
audioPlayer.setup()is called and awaited before usingadd(),play(),getSnapshot(), or other player APIs. - If an operation reports
player_not_setup, check that setup is not skipped by an app lifecycle branch and that initialization completes before user actions.
Remote events are not received
Section titled “Remote events are not received”- Verify
mediaSession.setup()was called;audioPlayer.setup()alone does not prepare remote-control listeners. - Verify listeners are registered through
mediaSessionor remote helper APIs (for exampleonRemotePlay) after setup.
Native command checks fail
Section titled “Native command checks fail”- Run
legato native doctorand review findings before debugging runtime behavior. - If you need a safe patch plan first, run
legato native configure --dry-run.
Expected result
Section titled “Expected result”After successful initialization:
audioPlayeris ready to accept playback commandsmediaSessionis ready to receive remote control events- Native platform services are running
You can verify the setup by querying the state:
const state = await audioPlayer.getState();console.log('Player state:', state); // 'idle'Next steps
Section titled “Next steps”- If you want to validate queue and playback commands first, continue with Play a Track.
- If you need UI/event-driven behavior next, continue with Listen to Events.
- If you need a local state mirror in app code, continue with Use Sync.
- If setup problems look platform-specific, check Native CLI and run doctor/configure flows.