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.
142 lines
4.0 KiB
142 lines
4.0 KiB
import 'dart:async';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:get/get.dart';
|
|
// 移除 speech_to_text 依赖
|
|
// import 'package:speech_to_text/speech_to_text.dart';
|
|
// import 'package:speech_to_text/speech_recognition_result.dart';
|
|
import '../../core/utils/logger.dart';
|
|
|
|
/// ASR状态枚举
|
|
enum AsrState {
|
|
notInitialized, // 未初始化
|
|
initialized, // 已初始化
|
|
listening, // 正在监听
|
|
notListening, // 未监听
|
|
error // 错误状态
|
|
}
|
|
|
|
/// 监听模式枚举(与 speech_to_text 兼容)
|
|
enum ListenMode {
|
|
deviceDefault,
|
|
dictation,
|
|
search,
|
|
confirmation,
|
|
}
|
|
|
|
/// 本地名称类(与 speech_to_text 兼容)
|
|
class LocaleName {
|
|
final String name;
|
|
final String localeId;
|
|
|
|
LocaleName(this.name, this.localeId);
|
|
}
|
|
|
|
/// Flutter ASR服务,负责本地语音转文本功能
|
|
/// 注意:此实现为空实现,不再依赖 speech_to_text 插件
|
|
class FlutterAsrService extends GetxService {
|
|
// 当前ASR状态
|
|
final Rx<AsrState> _asrState = AsrState.notInitialized.obs;
|
|
AsrState get asrState => _asrState.value;
|
|
|
|
// 可用的语音识别语言
|
|
final RxList<LocaleName> _locales = <LocaleName>[].obs;
|
|
List<LocaleName> get locales => _locales;
|
|
|
|
// 当前选择的语言
|
|
final RxString _currentLocale = ''.obs;
|
|
String get currentLocale => _currentLocale.value;
|
|
|
|
// 识别结果
|
|
final RxString _lastWords = ''.obs;
|
|
String get lastWords => _lastWords.value;
|
|
|
|
// 识别准确度
|
|
final RxDouble _confidence = 0.0.obs;
|
|
double get confidence => _confidence.value;
|
|
|
|
// 是否可用
|
|
final RxBool _isAvailable = false.obs;
|
|
bool get isAvailable => _isAvailable.value;
|
|
|
|
// 是否正在监听
|
|
bool _isListening = false;
|
|
bool get isListening => _isListening;
|
|
|
|
// 识别结果流
|
|
final _recognitionResultsController = StreamController<String>.broadcast();
|
|
Stream<String> get recognitionResults => _recognitionResultsController.stream;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
_initSpeech();
|
|
}
|
|
|
|
/// 初始化语音识别
|
|
Future<void> _initSpeech() async {
|
|
try {
|
|
// 空实现,不再使用 speech_to_text
|
|
_isAvailable.value = false;
|
|
_asrState.value = AsrState.error;
|
|
|
|
// 添加一个默认语言
|
|
_locales.add(LocaleName('中文 (简体)', 'zh-CN'));
|
|
_currentLocale.value = 'zh-CN';
|
|
|
|
Logger.info('FlutterAsrService 已初始化(空实现)');
|
|
} catch (e) {
|
|
_asrState.value = AsrState.error;
|
|
_isAvailable.value = false;
|
|
Logger.error('语音识别初始化失败: $e');
|
|
}
|
|
}
|
|
|
|
/// 开始监听
|
|
///
|
|
/// [listenFor] 监听时长,默认30秒
|
|
/// [pauseFor] 暂停时长,默认3秒,超过此时长无语音输入会自动停止
|
|
/// [partialResults] 是否返回部分结果,默认true
|
|
/// [onDevice] 是否使用设备本地识别,默认false
|
|
/// [listenMode] 监听模式,默认设备默认模式
|
|
Future<bool> startListening({
|
|
Duration listenFor = const Duration(seconds: 30),
|
|
Duration pauseFor = const Duration(seconds: 3),
|
|
bool partialResults = true,
|
|
bool onDevice = false,
|
|
ListenMode listenMode = ListenMode.deviceDefault,
|
|
}) async {
|
|
// 空实现,不再使用 speech_to_text
|
|
Logger.error('FlutterAsrService 为空实现,不支持语音识别');
|
|
return false;
|
|
}
|
|
|
|
/// 停止监听
|
|
Future<void> stopListening() async {
|
|
// 空实现,不再使用 speech_to_text
|
|
_isListening = false;
|
|
_asrState.value = AsrState.notListening;
|
|
}
|
|
|
|
/// 取消监听
|
|
Future<void> cancelListening() async {
|
|
// 空实现,不再使用 speech_to_text
|
|
_isListening = false;
|
|
_lastWords.value = '';
|
|
_confidence.value = 0.0;
|
|
_asrState.value = AsrState.notListening;
|
|
}
|
|
|
|
/// 设置当前语言
|
|
void setLocale(String localeId) {
|
|
if (_currentLocale.value != localeId) {
|
|
_currentLocale.value = localeId;
|
|
Logger.info('设置语音识别语言: $localeId');
|
|
}
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
_recognitionResultsController.close();
|
|
super.onClose();
|
|
}
|
|
}
|