@capawesome/capacitor-volume

中文文档
0.1.3发布于 2026/09/02MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 3646

Capacitor plugin to control the volume and observe hardware volume button presses on Android and iOS.

原始使用文档
文档目录39 节
  1. Features
  2. Use Cases
  3. Compatibility
  4. Installation
  5. Configuration
  6. Usage
  7. Get the current volume
  8. Set the volume
  9. Watch the hardware volume buttons
  10. Listen for volume button presses
  11. Listen for volume changes
  12. Remove all listeners
  13. API
  14. getVolume(...)
  15. isWatching()
  16. setVolume(...)
  17. startWatching(...)
  18. stopWatching()
  19. addListener('volumeButtonPressed', ...)
  20. addListener('volumeButtonReleased', ...)
  21. addListener('volumeChange', ...)
  22. removeAllListeners()
  23. Interfaces
  24. Type Aliases
  25. Enums
  26. Volume Control
  27. Volume Button Watching
  28. FAQ
  29. How is this plugin different from other similar plugins?
  30. Which platforms are supported by this plugin?
  31. Why is the stream option ignored on iOS?
  32. Why is setVolume rejected with the DO_NOT_DISTURB_ACCESS_RE…
  33. Why are volume button presses not detected?
  34. What does the suppressVolumeChange option do?
  35. Can I use this plugin with Ionic, React, Vue or Angular?
  36. Related Plugins
  37. Newsletter
  38. Changelog
  39. License

Capacitor Volume Plugin

Capacitor plugin to control the volume and observe hardware volume button presses.

Features

  • 🔊 Volume control: Get and set the volume level.
  • 🎚️ Audio streams: Control individual audio streams (music, ring, alarm and more) on Android.
  • 🔘 Volume buttons: Listen for hardware volume button presses.
  • 🤫 Suppression: Keep the volume unchanged and hide the system volume indicator while watching.
  • 👂 Change events: Listen for changes to the volume level.
  • 🤝 Compatibility: Works alongside the Audio Session, Media Session and Silent Mode plugins.
  • 📦 CocoaPods & SPM: Supports CocoaPods and Swift Package Manager for iOS.
  • 🔁 Up-to-date: Always supports the latest Capacitor version.

Missing a feature? Just open an issue and we'll take a look!

Use Cases

The Volume plugin is typically used whenever an app needs to control the volume or react to the hardware volume buttons, for example:

  • Media apps: Read and adjust the media volume from your own player UI.
  • Camera apps: Use the hardware volume buttons as a shutter trigger while keeping the volume unchanged.
  • Remote control apps: Map the volume buttons to custom actions, for example to control external devices.
  • Audio guidance: Warn users when the volume is too low to hear important audio cues.

Compatibility

Plugin VersionCapacitor VersionStatus
0.x.x>=8.x.xActive support

Installation

You can use our AI-Assisted Setup to install the plugin. Add the Capawesome Skills to your AI tool using the following command:

npx skills add capawesome-team/skills --skill capacitor-plugins

Then use the following prompt:

 Use the `capacitor-plugins` skill from `capawesome-team/skills` to install the `@capawesome/capacitor-volume` 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 @capawesome/capacitor-volume
npx cap sync

This plugin is only available on Android and iOS. On the Web, all methods reject as unimplemented.

Configuration

No configuration required for this plugin.

Usage

The following examples show how to get and set the current volume, watch the hardware volume buttons, listen for volume button presses and volume changes, and remove all listeners.

Get the current volume

Read the current volume level as a value between 0 and 1. On Android, you can select the audio stream (for example the ring stream) with the stream option. On iOS, this always returns the media volume:

import { Volume, VolumeStream } from '@capawesome/capacitor-volume';

const getVolume = async () => {
  const { volume } = await Volume.getVolume();
  return volume;
};

const getRingVolume = async () => {
  const { volume } = await Volume.getVolume({ stream: VolumeStream.Ring });
  return volume;
};

Set the volume

Set the volume level as a value between 0 and 1. On Android, you can select the audio stream with the stream option. On iOS, this always sets the media volume:

import { Volume } from '@capawesome/capacitor-volume';

const setVolume = async () => {
  await Volume.setVolume({ volume: 0.5 });
};

Watch the hardware volume buttons

Start watching the hardware volume buttons to receive the volumeButtonPressed and volumeChange events. With the suppressVolumeChange option enabled, the volume level is kept unchanged and the system volume indicator is hidden while watching:

import { Volume } from '@capawesome/capacitor-volume';

const startWatching = async () => {
  await Volume.startWatching({ suppressVolumeChange: true });
};

const stopWatching = async () => {
  await Volume.stopWatching();
};

const isWatching = async () => {
  const { watching } = await Volume.isWatching();
  return watching;
};

Listen for volume button presses

Get notified when a hardware volume button is pressed. The event is only emitted while watching (see above):

import { Volume } from '@capawesome/capacitor-volume';

const addVolumeButtonPressedListener = async () => {
  await Volume.addListener('volumeButtonPressed', event => {
    console.log('Volume button pressed:', event.direction);
  });
};

Listen for volume changes

Get notified when the volume level changes. The event is only emitted while watching (see above):

import { Volume } from '@capawesome/capacitor-volume';

const addVolumeChangeListener = async () => {
  await Volume.addListener('volumeChange', event => {
    console.log('Volume changed:', event.volume);
  });
};

Remove all listeners

Remove all listeners for this plugin when they are no longer needed:

import { Volume } from '@capawesome/capacitor-volume';

const removeAllListeners = async () => {
  await Volume.removeAllListeners();
};

API

getVolume(...)

getVolume(options?: GetVolumeOptions | undefined) => Promise<GetVolumeResult>

Get the current volume level.

On iOS, this always returns the media volume.

Only available on Android and iOS.

ParamType
optionsGetVolumeOptions

Returns: Promise<GetVolumeResult>

Since: 0.1.0


isWatching()

isWatching() => Promise<IsWatchingResult>

Check whether the hardware volume buttons are currently being watched.

Only available on Android and iOS.

Returns: Promise<IsWatchingResult>

Since: 0.1.0


setVolume(...)

setVolume(options: SetVolumeOptions) => Promise<void>

Set the volume level.

On iOS, this always sets the media volume.

Only available on Android and iOS.

ParamType
optionsSetVolumeOptions

Since: 0.1.0


startWatching(...)

startWatching(options?: StartWatchingOptions | undefined) => Promise<void>

Start watching the hardware volume buttons.

The volumeButtonPressed, volumeButtonReleased and volumeChange events are only emitted while watching.

If the volume buttons are already being watched, this call has no effect. Call stopWatching() first to change the options.

Only available on Android and iOS.

ParamType
optionsStartWatchingOptions

Since: 0.1.0


stopWatching()

stopWatching() => Promise<void>

Stop watching the hardware volume buttons.

On iOS, this also restores the volume level that was set when watching started if the suppressVolumeChange option was enabled.

Only available on Android and iOS.

Since: 0.1.0


addListener('volumeButtonPressed', ...)

addListener(eventName: 'volumeButtonPressed', listenerFunc: (event: VolumeButtonPressedEvent) => void) => Promise<PluginListenerHandle>

Called when a hardware volume button is pressed while watching.

Only available on Android and iOS.

ParamType
eventName'volumeButtonPressed'
listenerFunc(event: VolumeButtonPressedEvent) => void

Returns: Promise<PluginListenerHandle>

Since: 0.1.0


addListener('volumeButtonReleased', ...)

addListener(eventName: 'volumeButtonReleased', listenerFunc: (event: VolumeButtonReleasedEvent) => void) => Promise<PluginListenerHandle>

Called when a hardware volume button is released while watching.

Only available on Android.

ParamType
eventName'volumeButtonReleased'
listenerFunc(event: VolumeButtonReleasedEvent) => void

Returns: Promise<PluginListenerHandle>

Since: 0.2.0


addListener('volumeChange', ...)

addListener(eventName: 'volumeChange', listenerFunc: (event: VolumeChangeEvent) => void) => Promise<PluginListenerHandle>

Called when the volume level changes while watching.

On Android, this is called for changes to the music stream. On iOS, this is called for changes to the media volume and is not called while the suppressVolumeChange option is enabled.

Only available on Android and iOS.

ParamType
eventName'volumeChange'
listenerFunc(event: VolumeChangeEvent) => void

Returns: Promise<PluginListenerHandle>

Since: 0.1.0


removeAllListeners()

removeAllListeners() => Promise<void>

Remove all listeners for this plugin.

Since: 0.1.0


Interfaces

GetVolumeResult

PropTypeDescriptionSince
volumenumberThe current volume level as a value between 0 and 1.0.1.0

GetVolumeOptions

PropTypeDescriptionDefaultSince
streamVolumeStreamThe audio stream to get the volume for. Only available on Android.VolumeStream.Music0.1.0

IsWatchingResult

PropTypeDescriptionSince
watchingbooleanWhether the hardware volume buttons are currently being watched.0.1.0

SetVolumeOptions

PropTypeDescriptionDefaultSince
streamVolumeStreamThe audio stream to set the volume for. Only available on Android.VolumeStream.Music0.1.0
volumenumberThe volume level to set as a value between 0 and 1.0.1.0

StartWatchingOptions

PropTypeDescriptionDefaultSince
suppressVolumeChangebooleanWhether to keep the volume level unchanged when a hardware volume button is pressed while watching. On Android, the volume key events are consumed so that the system does not apply the volume change or display the volume panel. On iOS, the volume level is reset immediately after each button press and the system volume indicator is hidden. If the volume level is close to the minimum or maximum, it is nudged to a value from which both buttons can still be detected. The original volume level is restored when watching stops.false0.1.0

PluginListenerHandle

PropType
remove() => Promise<void>

VolumeButtonPressedEvent

PropTypeDescriptionSince
directionDirectionThe direction of the pressed hardware volume button.0.1.0

VolumeButtonReleasedEvent

PropTypeDescriptionSince
directionDirectionThe direction of the released hardware volume button.0.2.0

VolumeChangeEvent

PropTypeDescriptionSince
volumenumberThe new volume level as a value between 0 and 1.0.1.0

Type Aliases

Direction

The direction of a hardware volume button.

  • up: The volume up button.
  • down: The volume down button.

'down' | 'up'

Enums

VolumeStream

MembersValueDescriptionSince
Alarm'ALARM'The audio stream for alarms.0.1.0
Music'MUSIC'The audio stream for music and media playback.0.1.0
Notification'NOTIFICATION'The audio stream for notifications.0.1.0
Ring'RING'The audio stream for the phone ring.0.1.0
System'SYSTEM'The audio stream for system sounds.0.1.0
VoiceCall'VOICE_CALL'The audio stream for phone calls.0.1.0

Volume Control

Keep the following platform differences in mind when getting and setting the volume:

  • Android: The volume can be read and set per audio stream (see the stream option). If Do Not Disturb is active, changing the volume of the ring, notification or system stream requires Do Not Disturb access. Without this access, the call is rejected with the DO_NOT_DISTURB_ACCESS_REQUIRED error code. You can direct the user to the corresponding settings screen using the ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS intent, for example with the App Launcher plugin.
  • iOS: There is no public API to set the system volume directly. The plugin therefore uses a hidden system volume view to change the media volume, which is the only volume that can be read and set. The stream option is ignored.

Volume Button Watching

Keep the following platform differences in mind when watching the hardware volume buttons:

  • Android: The volume key events are intercepted by the web view, so button presses are only detected while the app is in the foreground. With the suppressVolumeChange option enabled, the key events are consumed, which keeps the volume unchanged and prevents the system volume panel from appearing.
  • iOS: Button presses are derived from changes to the media volume, so button presses are only detected while the app is in the foreground. Without the suppressVolumeChange option, presses can not be detected once the volume has reached its minimum or maximum. With the option enabled, the volume is reset immediately after each press (nudged away from the edges if needed), the system volume indicator is hidden, and the original volume is restored when watching stops. Note that volume changes from other sources (for example Control Center) are also reported while watching.

FAQ

How is this plugin different from other similar plugins?

It combines volume control and hardware button events in one unified API — a natural fit on iOS, where both rely on the same underlying system machinery. It adds per-stream control on Android and handles iOS volume-indicator suppression, all through a fully typed, actively maintained package, so a single dependency covers the whole volume story.

Which platforms are supported by this plugin?

The plugin is available on Android and iOS. On the Web, all methods reject as unimplemented.

Why is the stream option ignored on iOS?

On iOS, there is no public API to set the system volume directly. The plugin therefore uses a hidden system volume view to change the media volume, which is the only volume that can be read and set on iOS. The stream option is only supported on Android, see Volume Control for details.

Why is setVolume rejected with the DO_NOT_DISTURB_ACCESS_REQUIRED error code?

On Android, changing the volume of the ring, notification or system stream while Do Not Disturb is active requires Do Not Disturb access. You can direct the user to the corresponding settings screen using the ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS intent, for example with the App Launcher plugin, see Volume Control for details.

Why are volume button presses not detected?

The volumeButtonPressed event is only emitted while watching, so make sure you have called startWatching(...) first. On both platforms, button presses are only detected while the app is in the foreground. On iOS, presses can also not be detected once the volume has reached its minimum or maximum unless the suppressVolumeChange option is enabled, see Volume Button Watching for details.

What does the suppressVolumeChange option do?

With this option enabled, pressing a hardware volume button while watching does not change the volume and the system volume indicator stays hidden. On Android, the volume key events are consumed. On iOS, the volume level is reset immediately after each button press and the original volume level is restored when watching stops.

Can I use this plugin with Ionic, React, Vue or Angular?

Yes, the plugin is framework-agnostic. It works in any Capacitor app regardless of the web framework, including Ionic with Angular, React, or Vue, as well as plain JavaScript projects.

  • Audio Session: Configure and observe the iOS audio session.
  • Media Session: Interact with media controllers, volume keys and media buttons.
  • Silent Mode: Detect whether the device is in silent mode.

Newsletter

Stay up to date with the latest news and updates about the Capawesome, Capacitor, and Ionic ecosystem by subscribing to our Capawesome Newsletter.

Changelog

See CHANGELOG.md.

License

See LICENSE.

版本与发布记录

@capawesome/capacitor-volume 共发布 5 个版本,首次发布于 2026/07/07;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.3latest2026/09/022 个月前
0.1.22026/07/203 个月前
0.1.12026/07/153 个月前
0.1.02026/07/143 个月前
0.0.12026/07/074 个月前

关于 @capawesome/capacitor-volume 的常见问题

@capawesome/capacitor-volume 是什么?

Capacitor plugin to control the volume and observe hardware volume button presses on Android and iOS.

如何安装 @capawesome/capacitor-volume?

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

@capawesome/capacitor-volume 支持 TypeScript 吗?

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

@capawesome/capacitor-volume 支持 ESM 还是 CommonJS?

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

@capawesome/capacitor-volume 可以商用吗?

@capawesome/capacitor-volume 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@capawesome/capacitor-volume 的下载量有多少?

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

@capawesome/capacitor-volume 还在维护吗?

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