@capgo/capacitor-audio-session

中文文档
8.1.8发布于 2026/11/09MPL-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 4.3万

This capacitor plugin allows iOS applications to get notified audio about interrupts & route changes (for example when a headset is connected), and also query and override the audio device in use.

原始使用文档
文档目录16 节
  1. ➡️ Get started for free
  2. Key features
  3. Why Capacitor AudioSession?
  4. Documentation
  5. Compatibility
  6. Install
  7. API
  8. currentOutputs()
  9. overrideOutput(...)
  10. addListener('routeChanged', ...)
  11. addListener('interruption', ...)
  12. getPluginVersion()
  13. Interfaces
  14. Type Aliases
  15. Enums
  16. License

@capgo/capacitor-plugin-audiosession

Know when audio is interrupted or the output route changes on iOS, for example when headphones are plugged in, and switch output to the speaker. Keeps call, voice and media apps in control of sound.

Capgo - Build Capacitor apps with AI, ship updates instantly

Capgo: push fixes to your Capacitor users in minutes, build signed iOS and Android apps without a Mac, and roll back in one click.

➡️ Get started for free

14-day unlimited free trial. No credit card required

🤖 Building with an AI coding agent? Install Capgo skills so Claude Code, Codex, Cursor, Copilot, or Gemini CLI know how to install and use @capgo/capacitor-audio-session in your Capacitor app: npx skills add Cap-go/capgo-skills. Then ask your agent: "Add @capgo/capacitor-audio-session to my Capacitor app and wire it up." Agent setup guides · Hosted MCP

@capgo/capacitor-audio-session for Capacitor apps

Key features

  • Current route: currentOutputs() returns the active audio outputs.
  • Override output: overrideOutput() forces the speaker or returns to the default route.
  • Route changes: the routeChanged event fires when headphones or other devices connect or disconnect.
  • Interruptions: the interruption event fires when a call or another app interrupts audio.
  • Platforms: iOS. iOS only. Android and web are stubs.

This plugin works on iOS only.

This plugin is a port of https://github.com/saghul/cordova-plugin-audioroute and allows iOS applications to get notified about audio session interruptions and route changes (for example when a headset is connected). To query and override the audio device in use is also supported.

Why Capacitor AudioSession?

The only free and maintained iOS audio session plugin for Capacitor:

  • Audio route control - Detect and manage headphones, Bluetooth, speakers
  • Interruption handling - Respond to phone calls, alarms, and other interruptions
  • Output override - Force audio to speaker or restore default routing
  • Same JavaScript API - Compatible interface with paid alternatives
  • iOS-specific - Leverages AVAudioSession for deep iOS integration

Essential for VoIP apps, music players, podcast apps, and any app requiring precise audio routing control.

Documentation

The most complete doc is available here: https://capgo.app/docs/plugins/audiosession/

Compatibility

Plugin versionCapacitor compatibilityMaintained
v8.*.*v8.*.*✅
v7.*.*v7.*.*On demand
v6.*.*v6.*.*❌
v5.*.*v5.*.*❌

Note: The major version of this plugin follows the major version of Capacitor. Use the version that matches your Capacitor installation (e.g., plugin v8 for Capacitor 8). Only the latest major version is actively maintained.

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/capacitor-skills --skill capacitor-plugins

Then use the following prompt:

Use the `capacitor-plugins` skill from `cap-go/capacitor-skills` to install the `@capgo/capacitor-audio-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:

npm install @capgo/capacitor-plugin-audiosession
npx cap sync

For now this plugin works only in Capacitor 4.0+.

API

iOS-only plugin to query and control the audio session output and listen to route changes and interruptions.

currentOutputs()

currentOutputs() => Promise<AudioSessionPorts[]>

Get the current active audio output routes.

On web and non-iOS platforms, this resolves to an empty array.

Returns: Promise<AudioSessionPorts[]>


overrideOutput(...)

overrideOutput(type: OutputOverrideType) => Promise<OverrideResult>

Override the current audio output route.

Use speaker to force playback through the built-in speaker, or default to restore the system-selected route.

ParamTypeDescription
typeOutputOverrideTypeThe desired output override type.

Returns: Promise<OverrideResult>


addListener('routeChanged', ...)

addListener(eventName: 'routeChanged', listenerFunc: RouteChangeListener) => Promise<PluginListenerHandle>

Listen for audio route changes (e.g. headset connected/disconnected).

ParamTypeDescription
eventName'routeChanged'The route change event name.
listenerFuncRouteChangeListenerCallback invoked with the route change reason.

Returns: Promise<PluginListenerHandle>


addListener('interruption', ...)

addListener(eventName: 'interruption', listenerFunc: InterruptionListener) => Promise<PluginListenerHandle>

Listen for audio session interruptions (e.g. incoming call) and their end.

ParamTypeDescription
eventName'interruption'The interruption event name.
listenerFuncInterruptionListenerCallback invoked with the interruption type.

Returns: Promise<PluginListenerHandle>


getPluginVersion()

getPluginVersion() => Promise<{ version: string; }>

Get the native Capacitor plugin version

Returns: Promise<{ version: string; }>


Interfaces

PluginListenerHandle

PropType
remove() => Promise<void>

Type Aliases

OverrideResult

Result of an output override request.

{ success: boolean; message: string; }

OutputOverrideType

Output override type.

  • default: Use the system-selected route.
  • speaker: Force playback through the built-in speaker.

'default' | 'speaker'

RouteChangeListener

Listener called when the audio route changes.

(reason: RouteChangeReasons): void

InterruptionListener

Listener called when the audio session is interrupted or ends.

(type: InterruptionTypes): void

Enums

AudioSessionPorts

MembersValue
AIR_PLAY'airplay'
BLUETOOTH_LE'bluetooth-le'
BLUETOOTH_HFP'bluetooth-hfp'
BLUETOOTH_A2DP'bluetooth-a2dp'
BUILT_IN_SPEAKER'builtin-speaker'
BUILT_IN_RECEIVER'builtin-receiver'
HDMI'hdmi'
HEADPHONES'headphones'
LINE_OUT'line-out'

RouteChangeReasons

MembersValue
NEW_DEVICE_AVAILABLE'new-device-available'
OLD_DEVICE_UNAVAILABLE'old-device-unavailable'
CATEGORY_CHANGE'category-change'
OVERRIDE'override'
WAKE_FROM_SLEEP'wake-from-sleep'
NO_SUITABLE_ROUTE_FOR_CATEGORY'no-suitable-route-for-category'
ROUTE_CONFIGURATION_CHANGE'route-config-change'
UNKNOWN'unknown'

InterruptionTypes

MembersValue
BEGAN'began'
ENDED'ended'

License

MIT

版本与发布记录

@capgo/capacitor-audio-session 共发布 79 个版本,首次发布于 2025/08/21;过去 12 个月发布了 66 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.1.8latest2026/11/09今天
8.1.72026/11/026 天前
8.1.62026/10/2613 天前
8.1.52026/10/1920 天前
8.1.42026/10/1525 天前
8.1.32026/10/1327 天前

关于 @capgo/capacitor-audio-session 的常见问题

@capgo/capacitor-audio-session 是什么?

This capacitor plugin allows iOS applications to get notified audio about interrupts & route changes (for example when a headset is connected), and also query and override the audio device in use.

如何安装 @capgo/capacitor-audio-session?

在项目目录运行 npm install @capgo/capacitor-audio-session,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(>=8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@capgo/capacitor-audio-session 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@capgo/capacitor-audio-session 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@capgo/capacitor-audio-session 可以商用吗?

@capgo/capacitor-audio-session 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@capgo/capacitor-audio-session 的下载量有多少?

根据 npm 官方下载统计,@capgo/capacitor-audio-session 在 2026/10/09 – 2026/11/07 这 30 天内被下载 43,430 次,日均约 1,448 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@capgo/capacitor-audio-session 还在维护吗?

最新版本 8.1.8 发布于 2026/11/09(今天);至今共发布 79 个版本,其中过去 12 个月发布了 66 个;首次发布于 2025/08/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。