Getting Started
Dieser Inhalt ist in Ihrer Sprache noch nicht verfügbar.
-
Install the package
Terminal-Fenster npm i @capgo/capacitor-muteTerminal-Fenster pnpm add @capgo/capacitor-muteTerminal-Fenster yarn add @capgo/capacitor-muteTerminal-Fenster bun add @capgo/capacitor-mute -
Sync with native projects
Terminal-Fenster npx cap syncTerminal-Fenster pnpm cap syncTerminal-Fenster yarn cap syncTerminal-Fenster bunx cap sync
Usage
Import the plugin and use its methods to check mute state:
import { CapacitorMute } from '@capgo/capacitor-mute';
// Check if device is mutedconst checkMuteState = async () => { const { value } = await CapacitorMute.isMuted();
if (value) { console.log('Device is muted/silent'); // Adjust app behavior for silent mode } else { console.log('Device sound is on'); }};
// Listen for mute state changes (iOS only)const listenForChanges = async () => { await CapacitorMute.addListener('muteChanged', (state) => { console.log('Mute state changed:', state.value); updateUIForMuteState(state.value); });};
// Check if feature is availableconst checkAvailability = async () => { const { value } = await CapacitorMute.isAvailable(); console.log('Mute detection available:', value);};
API Reference
isMuted()
Checks if the device is currently muted/in silent mode.
const result = await CapacitorMute.isMuted();// Returns: { value: boolean }
isAvailable()
Checks if mute detection is available on the device.
const result = await CapacitorMute.isAvailable();// Returns: { value: boolean }
Events
muteChanged
Fired when the mute state changes (iOS only).
interface MuteChangedEvent { value: boolean;}
CapacitorMute.addListener('muteChanged', (event) => { console.log('Is muted:', event.value);});
Complete Example
import { CapacitorMute } from '@capgo/capacitor-mute';
export class SoundManager { private isMuted = false; private listener: any;
async initialize() { // Check if mute detection is available const { value: isAvailable } = await CapacitorMute.isAvailable();
if (!isAvailable) { console.log('Mute detection not available'); return; }
// Get initial mute state await this.checkMuteState();
// Listen for changes (iOS only) this.setupListener(); }
async checkMuteState() { try { const { value } = await CapacitorMute.isMuted(); this.isMuted = value; this.updateAppBehavior(); } catch (error) { console.error('Failed to check mute state:', error); } }
private setupListener() { this.listener = CapacitorMute.addListener('muteChanged', (event) => { this.isMuted = event.value; this.updateAppBehavior(); }); }
private updateAppBehavior() { if (this.isMuted) { // Device is muted - adjust app behavior this.disableSoundEffects(); this.showVisualNotifications(); } else { // Device sound is on this.enableSoundEffects(); this.useAudioNotifications(); } }
private disableSoundEffects() { // Disable in-app sounds console.log('Disabling sound effects'); }
private enableSoundEffects() { // Enable in-app sounds console.log('Enabling sound effects'); }
private showVisualNotifications() { // Use visual feedback instead of audio console.log('Using visual notifications'); }
private useAudioNotifications() { // Use audio notifications console.log('Using audio notifications'); }
cleanup() { if (this.listener) { this.listener.remove(); } }}
// Usageconst soundManager = new SoundManager();await soundManager.initialize();
Platform Behavior
iOS
- Detects the physical mute switch state
- Provides real-time change events
- Works on iPhone and iPad with mute switch
Android
- Checks if ringer mode is set to silent or vibrate
- No change events (polling required)
- Based on AudioManager ringer mode
Best Practices
-
Check availability first
const { value } = await CapacitorMute.isAvailable();if (!value) {// Fallback to default behavior} -
Respect user preferences Always honor the mute state and adjust your app’s audio behavior accordingly.
-
Provide visual alternatives
if (isMuted) {// Show visual notificationsshowToast('New message received');} else {// Play sound notificationplayNotificationSound();} -
Handle platform differences
import { Capacitor } from '@capacitor/core';if (Capacitor.getPlatform() === 'ios') {// Set up change listenerCapacitorMute.addListener('muteChanged', handler);} else {// Poll periodically on AndroidsetInterval(checkMuteState, 5000);}
Use Cases
-
Game Sound Management
const shouldPlaySound = async () => {const { value: isMuted } = await CapacitorMute.isMuted();return !isMuted && userPreferences.soundEnabled;}; -
Notification Handling
const sendNotification = async (message: string) => {const { value: isMuted } = await CapacitorMute.isMuted();if (isMuted) {// Use vibration or visual notificationawait Haptics.vibrate();showBanner(message);} else {// Play notification soundawait playSound('notification.mp3');}}; -
Video Player
const initVideoPlayer = async () => {const { value: isMuted } = await CapacitorMute.isMuted();videoPlayer.setMuted(isMuted);if (isMuted) {showSubtitles(true);showMuteIndicator();}};
Troubleshooting
-
Always returns false on Android
- Check if device ringer mode is actually set to silent
- Some Android devices may have different behavior
-
No change events on Android
- This is expected - Android doesn’t provide mute switch events
- Implement polling if real-time detection is needed
-
Not working on simulator
- iOS Simulator doesn’t have a mute switch
- Test on real devices for accurate results