Commencer
-
Installer the package
Fenêtre de terminal npm i @capgo/capacitor-muteFenêtre de terminal pnpm add @capgo/capacitor-muteFenêtre de terminal yarn add @capgo/capacitor-muteFenêtre de terminal bun add @capgo/capacitor-mute -
Synchroniser with Natif projects
Fenêtre de terminal npx cap syncFenêtre de terminal pnpm cap syncFenêtre de terminal yarn cap syncFenêtre de terminal bunx cap sync
Utilisation
Section titled “Utilisation”Importer the plugin and use its methods to Vérifier 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 Référence
Section titled “API Référence”isMuted()
Section titled “isMuted()”Checks if the Appareil is currently muted/in silent mode.
const result = await CapacitorMute.isMuted();// Returns: { value: boolean }Terminé Exemple
Section titled “Terminé Exemple”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
Bonnes pratiques
Section titled “Bonnes pratiques”-
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 Utilisateur preferences Always honor the mute state and adjust your Application’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();}};
Dépannage
Section titled “Dépannage”-
Always Retourne false on Android
- Vérifier if Appareil ringer mode is actually set to silent
- Some Android Appareils may have different behavior
-
No change events Disponible
- 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 Appareils for accurate results