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.

113 lines
2.4 KiB

import 'dart:async';
import 'dart:typed_data';
/// 语音识别服务接口
abstract class AstService {
/// 支持的语言
List<String> get supportedLanguages;
/// 初始化语音识别服务
Future<bool> initialize({required List<String> supportedLanguages});
/// 开始录音
Future<bool> enableRecord(String filePath);
///
Future<bool> startContinuousTranslation();
///
Future<bool> stopContinuousTranslation();
/// 返回一个包含识别事件的流
Future<Stream<RecognitionEvent1>> recognizeCallback();
/// 开始录音
Future<bool> path(String filePath);
/// 释放资源
Future<void> dispose();
}
// 识别事件类型
enum RecognitionEventType1 {
/// 最终识别结果
finalResult,
/// 中间识别结果(实时反馈)
intermediateResult,
/// 会话开始
sessionStarted,
/// 会话结束
sessionStopped,
/// 识别取消
canceled,
/// 识别错误
error,
}
// 识别事件
class RecognitionEvent1 {
/// 事件类型
final RecognitionEventType1 type;
/// 识别文本(仅在 finalResult 和 intermediateResult 类型中有效)
final String text;
/// 检测到的语言
final String detectedLanguage;
/// 角色
final String role;
/// 原始音频
final Uint8List? audio;
/// 错误信息(仅在 error 和 canceled 类型中有效)
final String error;
RecognitionEvent1({
required this.type,
this.text = '',
this.detectedLanguage = '',
this.role = '',
this.audio,
this.error = '',
});
/// 创建最终结果事件的快捷构造函数
factory RecognitionEvent1.finalResult({
required String text,
String detectedLanguage = '',
}) {
return RecognitionEvent1(
type: RecognitionEventType1.finalResult,
text: text,
detectedLanguage: detectedLanguage,
);
}
/// 创建错误事件的快捷构造函数
factory RecognitionEvent1.error(String errorMessage) {
return RecognitionEvent1(
type: RecognitionEventType1.error,
error: errorMessage,
);
}
/// 检查是否为最终结果
bool get isFinalResult => type == RecognitionEventType1.finalResult;
/// 检查是否为错误
bool get isError =>
type == RecognitionEventType1.error ||
type == RecognitionEventType1.canceled;
@override
String toString() {
return 'RecognitionEvent{type: $type, text: $text, detectedLanguage: $detectedLanguage, error: $error}';
}
}