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.
120 lines
3.5 KiB
120 lines
3.5 KiB
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import '../../../data/services/volcano_tts_service.dart';
|
|
|
|
class TtsTestController extends GetxController {
|
|
final VolcanoTtsService _ttsService = Get.find<VolcanoTtsService>();
|
|
|
|
// 文本控制器
|
|
final TextEditingController textController = TextEditingController();
|
|
|
|
// 可观察状态
|
|
final isPlaying = false.obs;
|
|
final errorMessage = ''.obs;
|
|
final selectedVoiceType = ''.obs;
|
|
final voiceTypes = <String>[].obs;
|
|
final isLoading = false.obs;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
|
|
// 监听TTS播放状态
|
|
_ttsService.playingStateStream.listen((playing) {
|
|
isPlaying.value = playing;
|
|
if (!playing) {
|
|
errorMessage.value = '';
|
|
}
|
|
});
|
|
|
|
// 设置默认文本
|
|
textController.text = '这是一个火山语音合成测试,请点击播放按钮听取合成效果。';
|
|
|
|
// 设置可用的语音类型
|
|
voiceTypes.value = [
|
|
// 趣味方言
|
|
'zh_female_wanqudashu_moon_bigtts', // 湾区大叔
|
|
'zh_female_daimengchuanmei_moon_bigtts', // 呆萌川妹
|
|
'zh_male_guozhoudege_moon_bigtts', // 广州德哥
|
|
'zh_male_beijingxiaoye_moon_bigtts', // 北京小爷
|
|
'zh_male_haoyuxiaoge_moon_bigtts', // 浩宇小哥
|
|
|
|
// 通用场景
|
|
'zh_male_shaonianzixin_moon_bigtts', // 少年梓辛/Brayan
|
|
|
|
// 角色扮演
|
|
'zh_female_meilinvyou_moon_bigtts', // 魅力女友
|
|
'zh_male_shenyeboke_moon_bigtts', // 深夜播客
|
|
'zh_female_sajiaonvyou_moon_bigtts', // 柔美女友
|
|
'zh_female_yuanqinvyou_moon_bigtts', // 撒娇学妹
|
|
|
|
];
|
|
|
|
// 设置当前选中的语音类型
|
|
selectedVoiceType.value = _ttsService.getCurrentVoiceType();
|
|
|
|
// 如果当前语音类型不在列表中,添加到列表
|
|
if (!voiceTypes.contains(selectedVoiceType.value)) {
|
|
voiceTypes.add(selectedVoiceType.value);
|
|
}
|
|
}
|
|
|
|
/// 播放文本
|
|
void speakText() async {
|
|
final text = textController.text.trim();
|
|
if (text.isEmpty) {
|
|
errorMessage.value = '请输入要合成的文本';
|
|
return;
|
|
}
|
|
|
|
errorMessage.value = '';
|
|
isLoading.value = true;
|
|
|
|
try {
|
|
// 获取当前语音类型
|
|
final currentVoiceType = selectedVoiceType.value;
|
|
|
|
// 直接使用选定的语音类型,不进行可用性检查
|
|
|
|
// 播放文本,传递选定的语音类型
|
|
await _ttsService.speak(text, voiceType: currentVoiceType);
|
|
} catch (e) {
|
|
errorMessage.value = e.toString();
|
|
isPlaying.value = false;
|
|
|
|
// 如果错误包含资源授权相关信息,提供更具体的提示
|
|
final errorMsg = e.toString().toLowerCase();
|
|
if (errorMsg.contains('resource not granted') ||
|
|
errorMsg.contains('403') ||
|
|
errorMsg.contains('授权') ||
|
|
errorMsg.contains('3001')) {
|
|
errorMessage.value = '语音类型授权错误: 您可能没有权限使用当前选择的语音类型,请尝试使用基础语音类型';
|
|
}
|
|
} finally {
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
|
|
/// 停止播放
|
|
void stopSpeaking() {
|
|
try {
|
|
_ttsService.stop();
|
|
} catch (e) {
|
|
errorMessage.value = e.toString();
|
|
}
|
|
}
|
|
|
|
/// 切换语音类型
|
|
void changeVoiceType(String voiceType) {
|
|
if (voiceType != selectedVoiceType.value) {
|
|
selectedVoiceType.value = voiceType;
|
|
errorMessage.value = '';
|
|
}
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
textController.dispose();
|
|
super.onClose();
|
|
}
|
|
}
|