简体   繁体   中英

Flutter: Google Speech-To-Text API always returns null

I'm trying to call google speech-to-text api but it always return me null result. I got the implementation hint from this answer: Using gcloud speech api for real-time speech recognition in dart, flutter

I'm using flutter_sound ( https://pub.dev/packages/flutter_sound ) package to record audio and then send base64 encoded audio to speech API

Code for recording audio

String path = await flutterSound.startRecorder(
        Platform.isIOS ? 'ios.' : 'android.aac',
        androidEncoder: AndroidEncoder.AAC,
        sampleRate: 16000 ,
        numChannels: 1,
        androidAudioSource: AndroidAudioSource.MIC,
      );
      print('startRecorder: $path');

The audio file android.aac with.aac extension is generated successfully from above code.

Below code is used for sending audio data to speech api

final _credentials = new ServiceAccountCredentials.fromJson(r'''
{
  "type": "service_account",
  "project_id": "",
  "private_key_id": "",
   ....

''');

  final _SCOPES = const [SpeechApi.CloudPlatformScope];

  void convert() async {
    clientViaServiceAccount(_credentials, _SCOPES).then((http_client) {
      var speech = new SpeechApi

      try{
        String myPath= _path;
        _readFileByte(myPath).then((bytesData) async {
          String audioString = base64.encode(bytesData);
          print('audioString: $audioString');
          String audioStringSample = "";
          RecognizeRequest r = RecognizeRequest();
          RecognitionAudio audio = RecognitionAudio.fromJson({ 'content': audioString});
          r.audio = audio;
          RecognitionConfig config = RecognitionConfig.fromJson({
            'languageCode' : 'en-US',
            'encoding' : 'LINEAR16',
            'sampleRateHertz' : 16000,
          });
          r.config = config;
          speech.speech.recognize(r).then((results) {
            for (var result in results.results) {
              print(result.alternatives[0].transcript);
            }
          });

        });
      } catch (e) {
        // if path invalid or not able to read
        print(e);
      }
    });
  }

  Future<Uint8List> _readFileByte(String filePath) async {
    Uri myUri = Uri.parse(filePath);
    File audioFile = File.fromUri(myUri);
    Uint8List bytes;
    await audioFile.readAsBytes().then((value) {
      bytes = Uint8List.fromList(value);
      print('reading of bytes is completed');
    }).catchError((onError) {
      print('Exception Error while reading audio from path:' +
          onError.toString());
    });
    return bytes;
  }

The above code works perfect with audioStringSample (Find sample audio content here: https://gist.github.com/DazWilkin/34d628b998b4266be818ffb3efd688aa ) but when I pass my own audio ie audioString the result is always null. Anything I am doing wrong here?

PS: I've also tried different encoding methods which are listed in Speech API reference ( https://cloud.google.com/speech-to-text/docs/encoding ) but remained unsuccessful.

The problem lied in the recorder library. The recorder which resolved the problem: https://pub.dev/packages/flutter_audio_recorder

I recently ran into this exact problem as well and I think the problem lies with the encoding of the file. I'm using v2.0.3 for flutter_sound and the default file type after recording is aac, however, according to https://cloud.google.com/speech-to-text/docs/encoding , they only acceptable file types are flac, amr, wav and some others.

I was using https://pub.dev/packages/google_speech and the preset encode is

'encoding': 'LINEAR16',

which explains why the wav file worked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM