第三方库flutter_tts:https://pub.dev/packages/flutter_tts
- 支持的语音:
ja-JP, el-GR, en-AU, ar-SA, hu-HU, sv-SE, zh-CN, fr-CA, en-US, it-IT, ro-RO, sk-SK, ko-KR, en-IE, zh-HK, fr-FR, nl-NL, id-ID, pt-BR, pt-PT, cs-CZ, en-GB, de-DE, da-DK, es-ES, pl-PL, ru-RU, zh-TW, es-MX, en-ZA, hi-IN, en-IN, th-TH, no-NO, tr-TR, fi-FI, nl-BE, he-IL - 支持设置音量
- 支持设置语速
- 支持设置音调
- 支持暂定
- 支持结束
import 'package:flutter_tts/flutter_tts.dart';
class TTSUtil {
TTSUtil._();
static TTSUtil _manager;
factory TTSUtil() {
if (_manager == null) {
_manager = TTSUtil._();
}
return _manager;
}
FlutterTts flutterTts;
initTTS() {
flutterTts = FlutterTts();
}
Future speak(String text) async {
/// 设置语言
await flutterTts.setLanguage("zh-CN");
/// 设置音量
await flutterTts.setVolume(0.8);
/// 设置语速
await flutterTts.setSpeechRate(0.5);
/// 音调
await flutterTts.setPitch(1.0);
// text = "你好,我的名字是李磊,你是不是韩梅梅?";
if (text != null) {
if (text.isNotEmpty) {
await flutterTts.speak(text);
}
}
}
/// 暂停
Future _pause() async {
await flutterTts.pause();
}
/// 结束
Future _stop() async {
await flutterTts.stop();
}
}