feat: 添加唤醒功能
This commit is contained in:
@@ -3,6 +3,7 @@ import 'dart:io';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_vosk_wakeword/flutter_vosk_wakeword.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:porcupine_flutter/porcupine_error.dart';
|
||||
import 'package:porcupine_flutter/porcupine_manager.dart';
|
||||
@@ -86,6 +87,36 @@ class AIChatAssistantManager {
|
||||
return file.path;
|
||||
}
|
||||
|
||||
Future<void> startVoskWakeword() async {
|
||||
FlutterVoskWakeword.instance.initialize(
|
||||
wakeWords: ['你好众众', '你好', '众众'], // 唤醒词列表
|
||||
);
|
||||
|
||||
debugPrint('Starting Vosk Wakeword detection...');
|
||||
FlutterVoskWakeword.instance.start();
|
||||
|
||||
FlutterVoskWakeword.instance.recognitionStream.listen((event) {
|
||||
debugPrint('Vosk Wakeword detected: ${event.text}');
|
||||
_wakeWordDetected = true;
|
||||
// 通知所有监听者
|
||||
// _wakeWordController.add(null);
|
||||
|
||||
// 可选:一段时间后自动重置状态
|
||||
Future.delayed(const Duration(seconds: 2), () {
|
||||
_wakeWordDetected = false;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> stopVoskWakeword() async {
|
||||
if (_wakeWordDetected) {
|
||||
_wakeWordDetected = false;
|
||||
}
|
||||
if (FlutterVoskWakeword.instance != null) {
|
||||
FlutterVoskWakeword.instance.stop();
|
||||
}
|
||||
}
|
||||
|
||||
/// 开启或关闭唤醒词检测
|
||||
Future<void> setWakeWordDetection(bool enable) async {
|
||||
if (enable == _isWakeWordEnabled && _porcupineManager != null) {
|
||||
@@ -148,5 +179,7 @@ class AIChatAssistantManager {
|
||||
_commandCubit = null;
|
||||
_wakeWordController.close();
|
||||
setWakeWordDetection(false); // 确保 Porcupine 停止并释放
|
||||
FlutterVoskWakeword.instance.dispose();
|
||||
debugPrint('AIChatAssistant 单例销毁');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user