Skip to main content

Media Session Capacitor plugin

Control media playback from lock screen and notification center

Install

bun add @capgo/capacitor-media-session bunx cap sync
npm, pnpm or yarn
  • npm install @capgo/capacitor-media-session
  • pnpm add @capgo/capacitor-media-session
  • yarn add @capgo/capacitor-media-session

Guide

How to use Media Session in Capacitor

Test on device

Download the Capgo app, then scan the QR code.

Media Session plugin preview QR code

Using @capgo/capacitor-media-session

Capacitor plugin to expose media session controls of the device.

Install

bun add @capgo/capacitor-media-session
bunx cap sync

bunx cap sync copies the plugin's native code into your native projects. Run it again after every plugin upgrade.

Import

import { MediaSession } from '@capgo/capacitor-media-session';

API at a glance

Method Description
setMetadata Sets metadata of the currently playing media.
setPlaybackState Updates the playback state of the media session.
setActionHandler Registers a handler for a media session action.
setPositionState Updates position state for the active media session.

Examples

setMetadata()

Sets metadata of the currently playing media.

import { MediaSession } from '@capgo/capacitor-media-session';

await MediaSession.setMetadata({ album: 'album' });

setPlaybackState()

Updates the playback state of the media session.

import { MediaSession } from '@capgo/capacitor-media-session';

await MediaSession.setPlaybackState({ playbackState: 'none' });

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()

Updates position state for the active media session.

import { MediaSession } from '@capgo/capacitor-media-session';

await MediaSession.setPositionState({
  duration: 1000,
  playbackRate: 1,
  position: 10,
});

Full reference

Keep going from Using @capgo/capacitor-media-session

If you are using Using @capgo/capacitor-media-session to plan dashboard and API operations, connect it with @capgo/capacitor-media-session for the implementation detail in @capgo/capacitor-media-session, Getting Started for the implementation detail in Getting Started, API Overview for the implementation detail in API Overview, Introduction for the implementation detail in Introduction, and API Keys for the implementation detail in API Keys.

FAQ

Media Session plugin FAQ

How do I install the Media Session plugin in a Capacitor app?

Run "bun add @capgo/capacitor-media-session" (or "npm install @capgo/capacitor-media-session"), then run "bunx cap sync" so the iOS and Android projects pick up the native code. Import it from "@capgo/capacitor-media-session" in your app code.

Does @capgo/capacitor-media-session work with React, Vue and Angular?

Yes. Media Session is a Capacitor package, so it works with any web framework that runs inside Capacitor, including Ionic, React, Vue, Angular, Svelte and plain JavaScript.

Which Capacitor version does @capgo/capacitor-media-session support?

Capgo plugins follow Capacitor's major version: use the plugin major version that matches your Capacitor major version (for example plugin v8 with Capacitor 8). The compatibility table in the GitHub README lists the maintained versions.

Is @capgo/capacitor-media-session free and open source?

Yes. The source code is public on GitHub at https://github.com/Cap-go/capacitor-media-session/ and the package is free to install from npm. Bug reports and pull requests are welcome.

Can I update code that uses Media Session without an App Store review?

Installing or upgrading the plugin changes native code, so it needs a new store build. After that, JavaScript, HTML and CSS changes that call the plugin can ship instantly with Capgo live updates.

Ship Media Session changes without waiting for app review

Once the plugin is in your store build, Capgo live updates push your JavaScript, HTML and CSS changes to users in minutes.

Start with Capgo