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.

87 lines
3.3 KiB

import 'package:get/get.dart';
import '../../core/controllers/permission_controller.dart';
import '../../data/services/audio_service.dart';
import '../../data/services/notification_service.dart';
import '../../data/services/volcano_ai_service.dart';
import '../../data/services/volcano_asr_service.dart';
import '../../data/services/volcano_tts_api_service.dart';
import '../../data/services/azure_asr_service.dart';
import '../../core/utils/logger.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
/// 初始绑定,用于管理全局依赖
class InitialBinding extends Bindings {
@override
void dependencies() {
// 初始化所有服务
final notificationService = Get.put(NotificationService(), permanent: true);
final audioService = Get.put(AudioServiceManager(), permanent: true);
final volcanoAiService = Get.put(VolcanoAIService(), permanent: true);
final voiceRecognitionService = Get.put(VolcanoAsrService(), permanent: true);
final volcanoTtsService = Get.put(VolcanoTtsApiService(), permanent: true);
final azureAsrService = Get.put(AzureAsrService(), permanent: true);
// 只注册全局控制器
Get.put(PermissionController(), permanent: true);
// 异步初始化各个服务,确保顺序执行
_initializeServices(notificationService, audioService);
// 初始化 Azure 服务
_initializeAzureService(azureAsrService);
}
// 单独提取初始化服务的方法,以便更好地处理错误
void _initializeServices(NotificationService notificationService, AudioServiceManager audioService) async {
try {
// 首先初始化通知服务
await notificationService.init().catchError((error) {
Logger.error('通知服务初始化失败', error);
return null;
});
// 等待一段时间,确保通知权限请求完成
await Future.delayed(Duration(seconds: 1));
// 然后初始化音频服务
await audioService.init().then((_) {
Logger.info('AudioServiceManager 初始化完成,可以接收蓝牙耳机按键事件');
});
} catch (e) {
Logger.error('服务初始化过程中发生错误', e);
}
// 其他服务初始化可以在这里添加
}
// 初始化 Azure 语音识别服务
void _initializeAzureService(AzureAsrService azureAsrService) async {
try {
// 从环境变量中获取 Azure 语音识别服务的配置参数
final subscriptionKey = dotenv.env['AZURE_ASR_SUBSCRIPTION_KEY'] ?? '';
final serviceRegion = dotenv.env['AZURE_ASR_SERVICE_REGION'] ?? 'eastasia';
final language = dotenv.env['AZURE_ASR_LANGUAGE'] ?? 'zh-CN';
// 检查配置是否完整
if (subscriptionKey.isEmpty) {
Logger.warning('Azure 语音识别服务的订阅密钥未设置,请检查 .env 文件');
return;
}
// 初始化 Azure ASR 服务
final success = await azureAsrService.initialize(
subscriptionKey: subscriptionKey,
serviceRegion: serviceRegion,
language: language,
);
if (success) {
Logger.info('Azure 语音识别服务初始化成功');
} else {
Logger.error('Azure 语音识别服务初始化失败');
}
} catch (e) {
Logger.error('初始化 Azure 语音识别服务失败', e);
}
}
}