title: Can I change the voice for Google TTS in Flutter?
body: <p>I've written a function that takes in 2 strings (statement, question) and uses <strong>Google TTS</strong> to read that out loud.</p>
  <p>I'm using the <code>flutter_tts: ^1.3.0</code> package and tried the <code>setVoice</code> method to change the voice of the speaker to one of the voices supported in <a href="https://cloud.google.com/text-to-speech/docs/voices" rel="nofollow noreferrer">google voices</a>:</p>
  <p>Here is my code:</p>
  <pre class="lang-dart prettyprint-override"><code>Future _speak(statement, question) async {
      flutterTts.setLanguage(&quot;cmn-CN&quot;);
      flutterTts.setVoice(&quot;cmn-CN-Standard-B&quot;);
      flutterTts.setSpeechRate(0.7);
      await flutterTts.speak(statement + question);
    }
  </code></pre>
  <p>The function works inasmuch as the text is getting read, but I'm getting an error on the <code>setVoice</code> method that says:</p>
  <pre><code>D/TTS     (12461): Voice name not found: cmn-CN-Standard-B
  </code></pre>
  <p>Can someone help with this, please? Thank you!</p>
  <hr />
  <p>UPDATE</p>
  <p>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.</p>
  <pre><code>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 = &quot;AIzaSyDWx34PZW0hjSpwExBo5bwrENvyRkLisBE&quot;;
  AudioPlayer audioPlayer = AudioPlayer();
  
  const String femalevoice = &quot;cmn-CN-Standard-A&quot;;
  const String malevoice = &quot;cmn-CN-Standard-B&quot;;
  
  Future&lt;http.Response&gt; texttospeech(String text, String voicetype) {
    String url =
        &quot;https://texttospeech.googleapis.com/v1beta1/text:synthesize?key=$_apikey&quot;;
  
    var body = json.encode({
      &quot;audioConfig&quot;: {&quot;audioEncoding&quot;: &quot;LINEAR16&quot;, &quot;pitch&quot;: 0, &quot;speakingRate&quot;: 1},
      &quot;input&quot;: {&quot;text&quot;: text},
      &quot;voice&quot;: {&quot;languageCode&quot;: &quot;cmn-CN&quot;, &quot;name&quot;: voicetype}
    });
  
    var response =
        http.post(url, headers: {&quot;Content-type&quot;: &quot;application/json&quot;}, 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(&quot;$dir/&quot; + DateTime.now().millisecondsSinceEpoch.toString() + &quot;.mp3&quot;);
  
    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(&quot;$dir/&quot; + DateTime.now().millisecondsSinceEpoch.toString() + &quot;.mp3&quot;);
  
    await file.writeAsBytes(bytes);
  
    int result = await audioPlayer.play(file.path);
    audioPlayer.setPlaybackRate(playbackRate: 0.7);
    audioPlayer.setVolume(1);
    if (result == 1) {
      // success
    }
  }
  </code></pre>