You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.3 KiB
64 lines
1.3 KiB
import 'package:get/get.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
class VoiceInputController extends GetxController {
|
|
// Observable states
|
|
final isConnecting = true.obs;
|
|
final isMuted = false.obs;
|
|
final isRecording = false.obs;
|
|
final recognizedText = ''.obs;
|
|
|
|
// Callbacks
|
|
final Function(String) onRecordingResult;
|
|
final VoidCallback onClosePanel;
|
|
final Function(String) onRecognizing;
|
|
|
|
VoiceInputController({
|
|
required this.onRecordingResult,
|
|
required this.onClosePanel,
|
|
required this.onRecognizing,
|
|
});
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
// 初始化完成后更新状态
|
|
isConnecting.value = false;
|
|
}
|
|
|
|
Future<void> startRecording() async {
|
|
try {
|
|
isRecording.value = true;
|
|
recognizedText.value = '';
|
|
} catch (e) {
|
|
Get.snackbar(
|
|
'Error',
|
|
'启动语音识别失败: $e',
|
|
snackPosition: SnackPosition.BOTTOM,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> stopRecording() async {
|
|
if (!isRecording.value) return;
|
|
isRecording.value = false;
|
|
}
|
|
|
|
void toggleMute() {
|
|
isMuted.value = !isMuted.value;
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// 停止录音
|
|
stopRecording();
|
|
|
|
// 重置状态
|
|
isConnecting.value = false;
|
|
isMuted.value = false;
|
|
isRecording.value = false;
|
|
recognizedText.value = '';
|
|
|
|
super.onClose();
|
|
}
|
|
}
|