fix: Improve microphone permission handling and audio capture robustness

- Add device enumeration check before attempting to capture audio
- Use simpler audio constraints (audio: true) instead of specific options
- Add fallback to explicit device ID if simple constraints fail
- Add more descriptive error messages for different failure modes
- Enhance Electron permission handlers with better logging
- Add setDevicePermissionHandler for audio device access
- Include 'microphone' in allowed permissions list

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
egg
2025-12-17 16:47:09 +08:00
parent e565951bf6
commit 49dba2c43e
2 changed files with 46 additions and 8 deletions

View File

@@ -405,9 +405,26 @@
return;
}
mediaStream = await navigator.mediaDevices.getUserMedia({
audio: { echoCancellation: true, noiseSuppression: true }
});
// Check for available audio devices first
const devices = await navigator.mediaDevices.enumerateDevices();
const audioInputs = devices.filter(d => d.kind === 'audioinput');
console.log('Available audio inputs:', audioInputs.length, audioInputs);
if (audioInputs.length === 0) {
alert('No microphone found. Please connect a microphone and try again.');
return;
}
// Try with simple constraints first, fall back to more specific ones
try {
mediaStream = await navigator.mediaDevices.getUserMedia({ audio: true });
} catch (simpleErr) {
console.warn('Simple audio constraints failed, trying with device ID:', simpleErr);
// Try with explicit device ID
mediaStream = await navigator.mediaDevices.getUserMedia({
audio: { deviceId: audioInputs[0].deviceId }
});
}
isRecording = true;
recordBtn.textContent = 'Stop Recording';
@@ -422,7 +439,15 @@
} catch (error) {
console.error('Start recording error:', error);
alert('Error starting recording: ' + error.message);
let errorMsg = 'Error starting recording: ' + error.message;
if (error.name === 'NotAllowedError') {
errorMsg = 'Microphone access denied. Please grant permission and try again.';
} else if (error.name === 'NotFoundError') {
errorMsg = 'No microphone found. Please connect a microphone and try again.';
} else if (error.name === 'NotReadableError') {
errorMsg = 'Microphone is in use by another application. Please close other apps using the microphone.';
}
alert(errorMsg);
await cleanupRecording();
}
}