开始使用
-
安装包
Terminal window npm i @capgo/capacitor-muteTerminal window pnpm add @capgo/capacitor-muteTerminal window yarn add @capgo/capacitor-muteTerminal window bun add @capgo/capacitor-mute -
与原生项目同步
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync
导入插件并使用其方法检查静音状态:
import { CapacitorMute } from '@capgo/capacitor-mute';
// 检查设备是否静音const checkMuteState = async () => { const { value } = await CapacitorMute.isMuted();
if (value) { console.log('Device is muted/silent'); // 调整静音模式下的应用行为 } else { console.log('Device sound is on'); }};API 参考
Section titled “API 参考”isMuted()
Section titled “isMuted()”检查设备当前是否静音/处于静音模式。
const result = await CapacitorMute.isMuted();// 返回:{ value: boolean }import { CapacitorMute } from '@capgo/capacitor-mute';
export class SoundManager { private isMuted = false;
async initialize() { // 获取初始静音状态 await this.checkMuteState(); }
async checkMuteState() { try { const { value } = await CapacitorMute.isMuted(); this.isMuted = value; this.updateAppBehavior(); } catch (error) { console.error('Failed to check mute state:', error); } }
private updateAppBehavior() { if (this.isMuted) { // 设备已静音 - 调整应用行为 this.disableSoundEffects(); this.showVisualNotifications(); } else { // 设备声音已打开 this.enableSoundEffects(); this.useAudioNotifications(); } }
private disableSoundEffects() { // 禁用应用内声音 console.log('Disabling sound effects'); }
private enableSoundEffects() { // 启用应用内声音 console.log('Enabling sound effects'); }
private showVisualNotifications() { // 使用视觉反馈而不是音频 console.log('Using visual notifications'); }
private useAudioNotifications() { // 使用音频通知 console.log('Using audio notifications'); }}
// 用法const soundManager = new SoundManager();await soundManager.initialize();
// 定期轮询以检查更改setInterval(() => { soundManager.checkMuteState();}, 5000); // 每 5 秒检查一次- 检测物理静音开关状态
- 无实时更改事件(需要轮询)
- 适用于带有静音开关的 iPhone 和 iPad
Android
Section titled “Android”- 检查铃声模式是否设置为静音或振动
- 无更改事件(需要轮询)
- 基于 AudioManager 铃声模式
-
轮询更改 由于插件不提供实时事件,如果需要跟踪更改,请定期轮询:
// 定期检查静音状态setInterval(async () => {const { value } = await CapacitorMute.isMuted();if (value !== previousMuteState) {handleMuteStateChange(value);}}, 5000); -
尊重用户偏好 始终尊重静音状态并相应调整应用的音频行为。
-
提供视觉替代方案
const { value: isMuted } = await CapacitorMute.isMuted();if (isMuted) {// 显示视觉通知showToast('New message received');} else {// 播放声音通知playNotificationSound();}
-
游戏声音管理
const shouldPlaySound = async () => {const { value: isMuted } = await CapacitorMute.isMuted();return !isMuted && userPreferences.soundEnabled;}; -
通知处理
const sendNotification = async (message: string) => {const { value: isMuted } = await CapacitorMute.isMuted();if (isMuted) {// 使用振动或视觉通知await Haptics.vibrate();showBanner(message);} else {// 播放通知声音await playSound('notification.mp3');}}; -
视频播放器
const initVideoPlayer = async () => {const { value: isMuted } = await CapacitorMute.isMuted();videoPlayer.setMuted(isMuted);if (isMuted) {showSubtitles(true);showMuteIndicator();}};
-
在 Android 上始终返回 false
- 检查设备铃声模式是否实际设置为静音
- 某些 Android 设备可能有不同的行为
-
没有可用的更改事件
- 插件不提供实时更改事件
- 如果需要检测更改,请实现轮询
-
在模拟器上不起作用
- iOS 模拟器没有静音开关
- 在真实设备上测试以获得准确结果