toggleMic method
Implementation
Future<void> toggleMic() async {
try {
if (!_isInitialized) {
print('Service not initialized. Reinitializing...');
await _initializeWithRetry();
}
if (!_isInitialized) {
throw Exception('Speech recognition service is not initialized');
}
final hasPermission = await checkMicrophonePermission();
if (!hasPermission) {
throw Exception('Microphone permission denied');
}
_isMicOn = !_isMicOn;
if (_isMicOn) {
// Start recording when mic is turned ON
AzureSpeechRecognition.continuousRecording();
onListeningStateChanged?.call(true);
} else {
// Stop recording and clear intermediate results when mic is turned OFF
await AzureSpeechRecognition.stopContinuousRecognition();
_intermediateResult = '';
textController.text = _recognizedText.trim();
onListeningStateChanged?.call(false);
}
} catch (e) {
print('Error toggling microphone: $e');
onError?.call('Failed to toggle microphone: ${e.toString()}');
_isMicOn = false;
onListeningStateChanged?.call(false);
rethrow;
}
}