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.
70 lines
2.1 KiB
70 lines
2.1 KiB
import 'package:flutter/services.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
|
|
|
/// 语音交互服务,用于管理原生的 VoiceInteractionService
|
|
class VoiceInteractionService extends GetxService {
|
|
static VoiceInteractionService get to => Get.find();
|
|
|
|
// 服务运行状态
|
|
final _isRunning = false.obs;
|
|
bool get isRunning => _isRunning.value;
|
|
|
|
// 方法通道
|
|
final MethodChannel _channel = const MethodChannel('com.deep_voice.voice_interaction');
|
|
|
|
/// 初始化服务
|
|
Future<VoiceInteractionService> init() async {
|
|
// 获取服务运行状态
|
|
await _updateServiceStatus();
|
|
return this;
|
|
}
|
|
|
|
/// 启动语音交互服务
|
|
Future<bool> startService() async {
|
|
try {
|
|
// 获取配置信息
|
|
final speechKey = dotenv.env['AZURE_ASR_SUBSCRIPTION_KEY'] ?? '';
|
|
final speechRegion = dotenv.env['AZURE_ASR_SERVICE_REGION'] ?? '';
|
|
|
|
// 从 .env 读取火山 AI API Key
|
|
final aiApiKey = dotenv.env['VOLCANO_AI_API_KEY'] ?? '';
|
|
|
|
// 调用原生方法,传递配置参数
|
|
final result = await _channel.invokeMethod<bool>('startVoiceInteractionService', {
|
|
'azure_speech_key': speechKey,
|
|
'azure_speech_region': speechRegion,
|
|
'volcano_ai_api_key': aiApiKey,
|
|
}) ?? false;
|
|
|
|
await _updateServiceStatus();
|
|
return result;
|
|
} catch (e) {
|
|
print('启动语音交互服务失败: $e');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// 停止语音交互服务
|
|
Future<bool> stopService() async {
|
|
try {
|
|
final result = await _channel.invokeMethod<bool>('stopVoiceInteractionService') ?? false;
|
|
await _updateServiceStatus();
|
|
return result;
|
|
} catch (e) {
|
|
print('停止语音交互服务失败: $e');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// 更新服务状态
|
|
Future<void> _updateServiceStatus() async {
|
|
try {
|
|
final status = await _channel.invokeMethod<bool>('isVoiceInteractionServiceRunning') ?? false;
|
|
_isRunning.value = status;
|
|
} catch (e) {
|
|
print('获取语音交互服务状态失败: $e');
|
|
_isRunning.value = false;
|
|
}
|
|
}
|
|
}
|