feat:使用本地tts
This commit is contained in:
1 parent
2490d4f9a0
commit
d656ce14eb
22 files changed
+184
-185
No files matched your search
@@ -39,6 +39,7 @@ class AppControl extends GetxController {
|
||||
serialCardManage.close();
|
||||
srialSteelyardManage.close();
|
||||
AppNetworkTool.closeListenNetwork();
|
||||
TtsManage.dispose();
|
||||
}
|
||||
|
||||
Future<void> initAsync() async {
|
||||
@@ -52,6 +53,7 @@ class AppControl extends GetxController {
|
||||
await getDatabasesPath(),
|
||||
);
|
||||
await AppFileTool.backupSqlFile();
|
||||
await TtsManage.initialize();
|
||||
Get.offAllNamed(RouteNames.login);
|
||||
}
|
||||
}
|
||||
@@ -27,9 +27,9 @@ export './manage/print_manage.dart';
|
||||
export './manage/serial_card_manage.dart';
|
||||
export './manage/serial_steelyard_manage.dart';
|
||||
export './manage/scan_code_manage.dart';
|
||||
export './manage/tts_manage.dart';
|
||||
|
||||
export './plugs/face_discern_plugs.dart';
|
||||
export './plugs/local_tts_plugs.dart';
|
||||
|
||||
export './router/router_observer_tool.dart';
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import 'package:flutter_tts/flutter_tts.dart';
|
||||
|
||||
// 语音转文字
|
||||
class TtsManage {
|
||||
static final FlutterTts _flutterTts = FlutterTts();
|
||||
static bool _isInitialized = false;
|
||||
|
||||
// 初始化TTS
|
||||
static Future<void> initialize() async {
|
||||
if (!_isInitialized) {
|
||||
// 设置语言为中文
|
||||
await _flutterTts.setLanguage("zh-CN");
|
||||
// 设置音量
|
||||
await _flutterTts.setVolume(1.0);
|
||||
// 设置语速
|
||||
await _flutterTts.setSpeechRate(0.7);
|
||||
// 设置音调
|
||||
await _flutterTts.setPitch(1.0);
|
||||
// 设置使用本地引擎
|
||||
await _flutterTts.setIosAudioCategory(
|
||||
IosTextToSpeechAudioCategory.ambient,
|
||||
[
|
||||
IosTextToSpeechAudioCategoryOptions.allowBluetooth,
|
||||
IosTextToSpeechAudioCategoryOptions.allowBluetoothA2DP
|
||||
],
|
||||
);
|
||||
|
||||
_isInitialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 播放文本
|
||||
static Future<void> speak(String text) async {
|
||||
await initialize();
|
||||
await _flutterTts.awaitSpeakCompletion(true);
|
||||
await _flutterTts.speak(text, focus: true);
|
||||
}
|
||||
|
||||
// 停止播放
|
||||
static Future<void> stop() async {
|
||||
await _flutterTts.stop();
|
||||
}
|
||||
|
||||
// 释放资源
|
||||
static Future<void> dispose() async {
|
||||
await _flutterTts.stop();
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'package:pj_supermarket_cloud/common/index.dart';
|
||||
|
||||
abstract class LocalTtsPlugs {
|
||||
static const String _tag = "本地tts插件";
|
||||
|
||||
static const MethodChannel _channel = MethodChannel(
|
||||
Constants.localTTSPlugsMethodChannel,
|
||||
);
|
||||
|
||||
static Future<bool> speeching({
|
||||
required String appId,
|
||||
required String str,
|
||||
String speed = "80",
|
||||
}) async {
|
||||
try {
|
||||
final pa = {
|
||||
"appId": appId,
|
||||
"str": str,
|
||||
"speed": speed,
|
||||
};
|
||||
await _channel.invokeMethod('speeching', pa);
|
||||
return true;
|
||||
} catch (e) {
|
||||
logger.w("|$_tag| jumpFaceDiscernPage 错误: $e");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user