Getting Started
Copy a setup prompt with the install steps and the full markdown guide for this plugin.
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-media-session`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/media-session/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
Install
Section titled “Install”You can use our AI-Assisted Setup to install the plugin. Add the Capgo skills to your AI tool using the following command:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-pluginsThen use the following prompt:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-media-session` plugin in my project.If you prefer Manual Setup, install the plugin by running the following commands and follow the platform-specific instructions below:
bun add @capgo/capacitor-media-sessionbunx cap syncImport
Section titled “Import”import { MediaSession } from '@capgo/capacitor-media-session';Quick start
Section titled “Quick start”Publish metadata and playback state when your player starts.
import { MediaSession } from '@capgo/capacitor-media-session';
await MediaSession.setMetadata({ title: 'Episode 12', artist: 'Capgo Podcast', album: 'Ship faster', artwork: [{ src: 'https://cdn.example.com/artwork.png', sizes: '512x512', type: 'image/png' }],});
await MediaSession.setPlaybackState({ playbackState: 'playing' });
await MediaSession.setPositionState({ duration: 3600, position: 120, playbackRate: 1,});Example: handle lock screen controls
Section titled “Example: handle lock screen controls”await MediaSession.setActionHandler({ action: 'play' }, () => { audio.play();});
await MediaSession.setActionHandler({ action: 'pause' }, () => { audio.pause();});
await MediaSession.setActionHandler({ action: 'seekto' }, (details) => { if (details.seekTime != null) { audio.currentTime = details.seekTime; }});Pass null as the handler to unregister an action.
Example: pause state when buffering
Section titled “Example: pause state when buffering”await MediaSession.setPlaybackState({ playbackState: 'paused' });// resume with 'playing' when your buffer is readyCommon pitfalls
Section titled “Common pitfalls”- Keep position fresh: call
setPositionStateas playback advances or scrubbing will feel broken on Android. - Artwork URLs: use HTTPS assets reachable from the device, large images slow down lock screen updates.
- Handler lifetime: register handlers once at player setup, not on every track change.
API Overview
Section titled “API Overview”setMetadata
Section titled “setMetadata”Sets metadata of the currently playing media.
import { MediaSession } from '@capgo/capacitor-media-session';
await MediaSession.setMetadata({ album: 'album' });setPlaybackState
Section titled “setPlaybackState”Updates the playback state of the media session.
import { MediaSession } from '@capgo/capacitor-media-session';
await MediaSession.setPlaybackState({ playbackState: 'none' });setActionHandler
Section titled “setActionHandler”Registers a handler for a media session action.
import { MediaSession } from '@capgo/capacitor-media-session';
await MediaSession.setActionHandler({ action: 'play' }, (details) => { console.log(details);});setPositionState
Section titled “setPositionState”Updates position state for the active media session.
import { MediaSession } from '@capgo/capacitor-media-session';
await MediaSession.setPositionState({ duration: 1000, playbackRate: 1, position: 10,});Type Reference
Section titled “Type Reference”MetadataOptions
Section titled “MetadataOptions”export interface MetadataOptions { album?: string; artist?: string; artwork?: MediaImage[]; title?: string;}PlaybackStateOptions
Section titled “PlaybackStateOptions”export interface PlaybackStateOptions { playbackState: MediaSessionPlaybackState;}ActionHandlerOptions
Section titled “ActionHandlerOptions”export interface ActionHandlerOptions { action: MediaSessionAction;}ActionHandler
Section titled “ActionHandler”export type ActionHandler = (details: ActionDetails) => void;PositionStateOptions
Section titled “PositionStateOptions”export interface PositionStateOptions { duration?: number; playbackRate?: number; position?: number;}MediaImage
Section titled “MediaImage”export interface MediaImage { src: string; sizes?: string; type?: string;}MediaSessionPlaybackState
Section titled “MediaSessionPlaybackState”export type MediaSessionPlaybackState = 'none' | 'paused' | 'playing';MediaSessionAction
Section titled “MediaSessionAction”export type MediaSessionAction = | 'play' | 'pause' | 'seekbackward' | 'seekforward' | 'previoustrack' | 'nexttrack' | 'seekto' | 'stop';Source Of Truth
Section titled “Source Of Truth”This page is generated from the plugin’s src/definitions.ts. Re-run the sync when the public API changes upstream.
Keep going from Getting Started
Section titled “Keep going from Getting Started”If you are using Getting Started to plan dashboard and API operations, connect it with Using @capgo/capacitor-media-session for the native capability in Using @capgo/capacitor-media-session, API Overview for the implementation detail in API Overview, Introduction for the implementation detail in Introduction, API Keys for the implementation detail in API Keys, and Devices for the implementation detail in Devices.