I've written a function that takes in 2 strings (statement, question) and uses Google TTS to read that out loud.
I'm using the flutter_tts: ^1.3.0
package and tried the setVoice
method to change the voice of the speaker to one of the voices supported in google voices:
Here is my code:
Future _speak(statement, question) async {
flutterTts.setLanguage("cmn-CN");
flutterTts.setVoice("cmn-CN-Standard-B");
flutterTts.setSpeechRate(0.7);
await flutterTts.speak(statement + question);
}
The function works inasmuch as the text is getting read, but I'm getting an error on the setVoice
method that says:
D/TTS (12461): Voice name not found: cmn-CN-Standard-B
Can someone help with this, please? Thank you!
UPDATE
I realised that I was not using the google TTS service, so I did that to implement both male and female voices. This code works for me now.
import 'dart:typed_data';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:path_provider/path_provider.dart';
import 'dart:io';
import 'package:audioplayers/audioplayers.dart';
var _apikey = "AIzaSyDWx34PZW0hjSpwExBo5bwrENvyRkLisBE";
AudioPlayer audioPlayer = AudioPlayer();
const String femalevoice = "cmn-CN-Standard-A";
const String malevoice = "cmn-CN-Standard-B";
Future<http.Response> texttospeech(String text, String voicetype) {
String url =
"https://texttospeech.googleapis.com/v1beta1/text:synthesize?key=$_apikey";
var body = json.encode({
"audioConfig": {"audioEncoding": "LINEAR16", "pitch": 0, "speakingRate": 1},
"input": {"text": text},
"voice": {"languageCode": "cmn-CN", "name": voicetype}
});
var response =
http.post(url, headers: {"Content-type": "application/json"}, body: body);
return response;
}
// Play male voice
playmalevoice(String text) async {
var response = await texttospeech(text, malevoice);
var jsonData = jsonDecode(response.body);
String audioBase64 = jsonData['audioContent'];
Uint8List bytes = base64Decode(audioBase64);
String dir = (await getApplicationDocumentsDirectory()).path;
File file =
File("$dir/" + DateTime.now().millisecondsSinceEpoch.toString() + ".mp3");
await file.writeAsBytes(bytes);
int result = await audioPlayer.play(file.path);
audioPlayer.setPlaybackRate(playbackRate: 0.7);
audioPlayer.setVolume(1);
if (result == 1) {
// success
}
}
// play female voice
playfemalevoice(String text) async {
var response = await texttospeech(text, femalevoice);
var jsonData = jsonDecode(response.body);
String audioBase64 = jsonData['audioContent'];
Uint8List bytes = base64Decode(audioBase64);
String dir = (await getApplicationDocumentsDirectory()).path;
File file =
File("$dir/" + DateTime.now().millisecondsSinceEpoch.toString() + ".mp3");
await file.writeAsBytes(bytes);
int result = await audioPlayer.play(file.path);
audioPlayer.setPlaybackRate(playbackRate: 0.7);
audioPlayer.setVolume(1);
if (result == 1) {
// success
}
}
With flutter tts, you can only set a voice that exists within the set of voices returned by getvoices().
The voices returned by getvoices() are going to be the voices that are available to the TTS Engine that is in use by the device running your app.
Flutter tts doesn't use google cloud speech -- it uses whatever happens to be on the device -- either the IOS speech engine, or any one of many possible tts engines on Android.
The only way to do what you're trying to do would be to use google cloud speech in your app instead of flutter tts, but this would mean your app would require internet, and possibly cost you money for the api key etc.