Can I change the voice for Google TTS in Flutter?
Asked Answered
P

2

7

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
  }
}
Polysepalous answered 16/11, 2020 at 15:33 Comment(1)
I tried await flutterTts.setVolume(volume); await flutterTts.setSpeechRate(rate); await flutterTts.setPitch(pitch); Map<String, String> reference = {'voice': "cmn-CN-Standard-B"}; await flutterTts.setVoice(reference); but setVoices did not change to the female voiceBattat
H
9

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.

Halloween answered 17/11, 2020 at 1:18 Comment(0)
W
0

await flutterTts.setVoice({'name': 'en-us-x-iol-local', 'locale': 'en-US'}); one of the male voice.

Whitt answered 24/7 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.