feat(voice-call): Improve socket connection and mic permission handling

This commit is contained in:
cogwheel0
2025-11-29 13:30:31 +05:30
parent 24bf1f06cb
commit 2ef49a2974
4 changed files with 83 additions and 8 deletions

View File

@@ -1,8 +1,10 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io' show Platform;
import 'dart:io' show File, Platform;
import 'dart:typed_data';
import 'package:path_provider/path_provider.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
@@ -343,6 +345,51 @@ class VoiceInputService {
}
}
/// Requests microphone permission if not already granted.
/// Returns true if permission is granted, false otherwise.
Future<bool> requestMicrophonePermission() async {
try {
// First check if we already have permission
var hasPermission = await _microphonePermissionProbe.hasPermission();
if (hasPermission) return true;
// The record package's start() method will trigger the system permission
// dialog if permission hasn't been granted yet. We start a brief recording
// and immediately stop it to trigger the permission request.
try {
// Create a temporary file path for the recording probe.
// An empty path only works on web; mobile platforms need a real path.
final tempDir = await getTemporaryDirectory();
final tempPath =
'${tempDir.path}/mic_permission_probe_${DateTime.now().millisecondsSinceEpoch}.wav';
await _microphonePermissionProbe.start(
const RecordConfig(encoder: AudioEncoder.wav),
path: tempPath,
);
await _microphonePermissionProbe.stop();
// Clean up the temporary file
try {
final tempFile = File(tempPath);
if (await tempFile.exists()) {
await tempFile.delete();
}
} catch (_) {
// Ignore cleanup errors
}
} catch (_) {
// Starting may fail if permission is denied, which is expected
}
// Check again after the permission request attempt
hasPermission = await _microphonePermissionProbe.hasPermission();
return hasPermission;
} catch (_) {
return false;
}
}
Future<void> _startLocalRecognition({
required bool allowOnlineFallback,
}) async {