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.
98 lines
2.2 KiB
98 lines
2.2 KiB
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
import '../../../data/services/asr_service.dart';
|
|
|
|
class MeetingController extends GetxController
|
|
with GetSingleTickerProviderStateMixin {
|
|
late AnimationController lottieController;
|
|
late AsrService _asrService;
|
|
|
|
Timer? timer;
|
|
|
|
RxBool isRecording = false.obs; //是否录音中
|
|
RxInt seconds = 0.obs;
|
|
|
|
RxString intermediateContent = ''.obs;
|
|
RxString finalContent = ''.obs;
|
|
|
|
StreamSubscription? _recognitionSubscription;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
lottieController = AnimationController(vsync: this);
|
|
_asrService = Get.find<AsrService>();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
lottieController.dispose();
|
|
timer?.cancel();
|
|
_asrService.stopContinuousRecognition();
|
|
_recognitionSubscription?.cancel();
|
|
super.onClose();
|
|
}
|
|
|
|
// 点击开始、暂停按钮
|
|
void recorderHandle() {
|
|
if (isRecording.value) {
|
|
_pause();
|
|
} else {
|
|
_start();
|
|
}
|
|
}
|
|
|
|
// 开始
|
|
void _start() async {
|
|
await _startRecorder();
|
|
lottieController.repeat();
|
|
isRecording.value = true;
|
|
timer = Timer.periodic(const Duration(seconds: 1), (_) => seconds++);
|
|
}
|
|
|
|
// 暂停
|
|
void _pause() async {
|
|
await _stopRecorder();
|
|
lottieController.stop();
|
|
isRecording.value = false;
|
|
timer?.cancel();
|
|
}
|
|
|
|
//开始录音
|
|
Future<void> _startRecorder() async {
|
|
final recognitionStream = await _asrService.startContinuousRecognition();
|
|
_recognitionSubscription = recognitionStream.listen(
|
|
_handleRecognitionEvent,
|
|
);
|
|
}
|
|
|
|
//结束录音
|
|
Future<void> _stopRecorder() async {
|
|
await _asrService.stopContinuousRecognition();
|
|
_recognitionSubscription?.cancel();
|
|
}
|
|
|
|
// 处理语音识别事件
|
|
void _handleRecognitionEvent(RecognitionEvent event) {
|
|
switch (event.type) {
|
|
case RecognitionEventType.intermediateResult:
|
|
intermediateContent.value = event.text;
|
|
break;
|
|
case RecognitionEventType.finalResult:
|
|
finalContent.value += event.text;
|
|
intermediateContent.value = '';
|
|
break;
|
|
default:
|
|
// 不做任何处理
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 完成
|
|
void complete() async {
|
|
Get.back();
|
|
}
|
|
}
|
|
|