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.
79 lines
2.4 KiB
79 lines
2.4 KiB
import 'dart:async';
|
|
import 'package:audio_service/audio_service.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'my_audio_handler.dart';
|
|
import '../../modules/chat/controllers/chat_controller.dart';
|
|
import '../../data/providers/agent_provider.dart';
|
|
import 'volcano_tts_service.dart';
|
|
|
|
class AudioServiceManager extends GetxService {
|
|
static AudioServiceManager get to => Get.find();
|
|
late MyAudioHandler _audioHandler;
|
|
StreamSubscription? _customEventSubscription;
|
|
|
|
Future<AudioServiceManager> init() async {
|
|
print('初始化 AudioService');
|
|
|
|
try {
|
|
// 初始化 AudioService
|
|
_audioHandler = await AudioService.init(
|
|
builder: () => MyAudioHandler(),
|
|
config: const AudioServiceConfig(
|
|
androidNotificationChannelId: 'com.example.deep_voice.channel.audio',
|
|
androidNotificationChannelName: 'Deep Voice Audio Service',
|
|
androidNotificationIcon: 'drawable/ic_notification',
|
|
androidNotificationOngoing: true,
|
|
androidStopForegroundOnPause: true,
|
|
androidShowNotificationBadge: true,
|
|
notificationColor: Colors.blue,
|
|
),
|
|
);
|
|
|
|
// 监听自定义事件
|
|
_customEventSubscription = _audioHandler.customEventStream.listen((event) {
|
|
if (event == 'activate_app') {
|
|
print('收到激活应用事件');
|
|
_activateAppWithPersonalAssistant();
|
|
}
|
|
});
|
|
|
|
return this;
|
|
} catch (e) {
|
|
print('AudioService初始化失败: $e');
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
void _activateAppWithPersonalAssistant() {
|
|
try {
|
|
// 获取 personal_assistant agent
|
|
final agent = AgentProvider.getAgentById('personal_assistant');
|
|
if (agent == null) {
|
|
print('未找到 personal_assistant agent');
|
|
return;
|
|
}
|
|
|
|
// 导航到聊天页面,传递 agent 参数
|
|
Get.toNamed('/chat', arguments: {
|
|
'agentId': agent.id,
|
|
'agentName': agent.name,
|
|
'agentAvatar': agent.avatarUrl,
|
|
'agentSubtitle': agent.description,
|
|
'systemPrompt': agent.systemPrompt,
|
|
'welcomeMessage': agent.welcomeMessage,
|
|
});
|
|
|
|
// ChatController 会自动处理欢迎消息和问候语的显示与播放
|
|
} catch (e) {
|
|
print('激活应用失败: $e');
|
|
}
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
_customEventSubscription?.cancel();
|
|
_audioHandler.stop();
|
|
super.onClose();
|
|
}
|
|
}
|