Getting Started
-
Install the package
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 -
Sync with native projects
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync
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'); }};API Reference
Section titled “API Reference”isMuted()
Section titled “isMuted()”Checks if the device is currently muted/in silent mode.
const result = await CapacitorMute.isMuted();// Returns: { value: boolean }Complete Example
Section titled “Complete Example”import { CapacitorMute } from '@capgo/capacitor-mute';
export class SoundManager { private isMuted = false;
async initialize() { // Get initial mute state 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) { // 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'); }}
// Usageconst soundManager = new SoundManager();await soundManager.initialize();
// Poll periodically to check for changessetInterval(() => { soundManager.checkMuteState();}, 5000); // Check every 5 secondsPlatform Behavior
Section titled “Platform Behavior”- Detects the physical mute switch state
- No real-time change events (polling required)
- Works on iPhone and iPad with mute switch
Android
Section titled “Android”- Checks if ringer mode is set to silent or vibrate
- No change events (polling required)
- Based on AudioManager ringer mode
Best Practices
Section titled “Best Practices”-
Poll for changes Since the plugin doesn’t provide real-time events, poll periodically if you need to track changes:
// Check mute state periodicallysetInterval(async () => {const { value } = await CapacitorMute.isMuted();if (value !== previousMuteState) {handleMuteStateChange(value);}}, 5000); -
Respect user preferences Always honor the mute state and adjust your app’s audio behavior accordingly.
-
Provide visual alternatives
const { value: isMuted } = await CapacitorMute.isMuted();if (isMuted) {// Show visual notificationsshowToast('New message received');} else {// Play sound notificationplayNotificationSound();}
Use Cases
Section titled “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
Section titled “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 available
- The plugin does not provide real-time change events
- Implement polling if you need to detect changes
-
Not working on simulator
- iOS Simulator doesn’t have a mute switch
- Test on real devices for accurate results