使用 @capgo/capacitor-volume-buttons
Capacitor Volume Buttons 插件,用于检测硬件音量键按下事件。
安装
bun add @capgo/capacitor-volume-buttons
bunx cap sync
bunx cap sync 将插件的原生 code 复制到你的原生项目中。每次升级插件后都要重新运行。
导入
import { VolumeButtons } from '@capgo/capacitor-volume-buttons';
API 概览
| 方法 | 描述 |
|---|---|
addListener |
监听硬件音量键的按下事件。 |
removeAllListeners |
移除该插件的所有监听器。 |
getPluginVersion |
获取原生 Capacitor 插件版本。 |
示例
addListener()
监听硬件音量键的按下事件。
import { VolumeButtons } from '@capgo/capacitor-volume-buttons';
const listener = await VolumeButtons.addListener(
'volumeButtonPressed',
(event) => {
console.log(`Volume ${event.direction} button pressed`);
}
);
// Remove listener when done
await listener.remove();
removeAllListeners()
移除该插件的所有监听器。
import { VolumeButtons } from '@capgo/capacitor-volume-buttons';
await VolumeButtons.removeAllListeners();
getPluginVersion()
获取原生 Capacitor 插件版本。
import { VolumeButtons } from '@capgo/capacitor-volume-buttons';
const { version } = await VolumeButtons.getPluginVersion();
console.log('Plugin version:', version);
监听事件
addListener 返回一个句柄。调用 handle.remove() 当页面卸载时,或 VolumeButtons.removeAllListeners() 以清除所有监听器。
完整参考
继续阅读:使用 @capgo/capacitor-volume-buttons
如果你正在使用 使用 @capgo/capacitor-volume-buttons 要规划原生插件工作,请将其连接到 @capgo/capacitor-volume-buttons 有关 @capgo/capacitor-volume-buttons 中的实现细节, 快速入门 有关入门指南中的实现细节, Capgo 插件目录 用于 Capgo 插件目录中的产品工作流, 由 Capgo 提供的 Capacitor 插件 有关 Capgo 提供的 Capacitor 插件实现细节,以及 添加或更新插件 有关添加或更新插件的实现细节。