Skip to main content

Microphone Permissions

Comprehensive solution for managing microphone permissions in voice-enabled web applications
The useMicrophonePermission hook provides a robust foundation for handling microphone permissions in voice-enabled applications. This guide covers implementation patterns, best practices, and complete examples for seamless voice interaction.

Core Concepts

Permission States

The hook manages six distinct permission states that cover all possible scenarios:

Permission Flow Visualization

The permission flow follows a predictable pattern that ensures smooth user experience:

Hook API Reference

Basic Usage

Import and use the hook to manage microphone permissions in your components:
Basic Hook Usage

Return Values

PermissionStateType
required
Current permission state. One of: idle, prompt, granted, denied, error, unsupported
string | null
Error message when state is error. Null otherwise.
MediaStream | null
Active media stream when permission is granted. Null otherwise.
boolean
Convenience boolean that’s true when state is granted.
boolean
Convenience boolean that’s true when state is denied.
boolean
Indicates whether user action is needed to grant permission.
() => Promise<PermissionCheckResult>
Checks current permission status without requesting access.
(callback?) => Promise<boolean>
Checks permission with automatic retry logic. Useful for waiting for user action.
() => Promise<void>
Requests microphone permission from the browser.
() => Promise<void>
Refreshes the current permission state.
() => void
Stops the media stream and performs cleanup.

Implementation Patterns

This is the recommended pattern for guide/walkthrough scenarios where you want full control over the user experience.

UI/UX Best Practices

Clear Permission Context

Explain the Why

Always explain WHY you need microphone access before requesting. Users are more likely to grant permissions when they understand the value.
Permission Context Example

Progressive Disclosure

Wait for User Intent

Don’t request permissions immediately on page load. Wait for explicit user action to avoid feeling intrusive.
Progressive Disclosure Example

State-Specific Messaging

Provide clear, actionable messages for each permission state:
State Message Handler

Error Handling

Common Error Scenarios

Handle different error types with specific solutions:
Error Handler

Graceful Degradation

Always provide fallback options when voice isn’t available:
Adaptive Guide Component

Complete Integration Example

Here’s a comprehensive implementation that combines all the best practices:

Common Pitfalls and Mistakes

These are critical mistakes that will cause your microphone permission flow to fail. Review this section carefully to avoid common implementation errors.

❌ Incorrect Implementation (Will Fail)

Broken Implementation - DO NOT USE

✅ Correct Implementation

Proper Permission Flow

Critical Requirements Checklist

Problem

Not using the useMicrophonePermission hook at all
Common Mistake

Solution

Always import and use the permission hook
Correct Approach

Key Implementation Points

Essential Implementation Pattern

The correct pattern ALWAYS includes these elements:
  1. Import and use useMicrophonePermission hook
  2. Check permission state before starting agent
  3. Handle permission request if needed
  4. Track loading/starting state
  5. Implement auto-start logic with useEffect
  6. Handle all permission states in UI
  7. Provide recovery options for denied/error states

Troubleshooting

Common Issues

1

Permission State Not Updating

Use the refresh() method to force a re-check of the permission state:
2

Browser Compatibility Issues

Check for browser support before attempting to use microphone features:
3

Handling Permission Reset

Re-check permissions when the browser tab becomes visible:
4

Stream Cleanup

Always ensure proper cleanup of media streams:

Best Practices

Best Practices Checklist

Follow these guidelines for optimal microphone permission handling:
  1. Always explain why you need microphone access before requesting
  2. Check permission state before attempting to request
  3. Handle all states explicitly with appropriate UI
  4. Provide clear instructions for permission recovery
  5. Test across browsers and handle compatibility issues
  6. Clean up resources properly when component unmounts
  7. Use polling sparingly - only when expecting user action
  8. Provide fallbacks for when voice isn’t available
  9. Log errors for debugging but show user-friendly messages
  10. Respect user choice - don’t repeatedly prompt if denied
Remember that microphone permission is a sensitive user action - always be transparent about why you need it and what you’ll use it for. The modal-based pattern with clear messaging and progressive disclosure provides the best user experience for most use cases.

Next Steps

Voice Activity Detection

Learn about VAD configuration for optimal voice detection

Audio Processing

Explore audio processing features and optimization

Error Handling

Comprehensive error handling strategies

Guides System

Implement voice-guided experiences