简体   繁体   English

Flutter:Google Speech-To-Text API 总是返回 null

[英]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.我正在尝试调用 google speech-to-text api,但它总是返回 null 结果。 I got the implementation hint from this answer: Using gcloud speech api for real-time speech recognition in dart, flutter我从这个答案中得到了实现提示: 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我正在使用 flutter_sound ( https://pub.dev/packages/flutter_sound ) 包来录制音频,然后将 base64 编码的音频发送到语音 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.上面的代码成功生成了扩展名为.aac的音频文件android.aac。

Below code is used for sending audio data to speech api下面的代码用于将音频数据发送到语音 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.上面的代码与audioStringSample完美配合(在此处查找示例音频内容: https ://gist.github.com/DazWilkin/34d628b998b4266be818ffb3efd688aa)但是当我传递自己的音频时,即audioString结果始终为空。 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. PS:我也尝试了不同的编码方法,这些方法在 Speech API 参考 ( https://cloud.google.com/speech-to-text/docs/encoding ) 中列出,但仍然不成功。

The problem lied in the recorder library.问题出在记录器库上。 The recorder which resolved the problem: https://pub.dev/packages/flutter_audio_recorder解决问题的录音机: 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.我正在为 flutter_sound 使用 v2.0.3,录制后的默认文件类型是 aac,但是,根据https://cloud.google.com/speech-to-text/docs/encoding ,它们唯一可接受的文件类型是 flac , amr, wav 和其他一些。

I was using https://pub.dev/packages/google_speech and the preset encode is我正在使用https://pub.dev/packages/google_speech并且预设编码是

'encoding': 'LINEAR16', '编码':'LINEAR16',

which explains why the wav file worked这解释了为什么 wav 文件有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何允许团队成员使用我的 Google Cloud Speech-to-Text API 帐户? - How do I allow a team member to use my Google Cloud Speech-to-Text API account? Speech-to-Text 短语超出字符数限制 - Speech-to-Text Phrase Exceeds Character Limit Azure 语音转文本忽略数字 - Azure speech-to-text ignores numbers Flutter 谷歌登录 - Email 数据返回 null - Flutter Google Sign In - Email data returns null Google Text to Speech:带减号的单词返回 Timepoint second = 0 - Google Text to Speech: word with minus character returns Timepoint second = 0 Google Cloud Text To Speech API 强制发音为单词 - Google Cloud Text To Speech API force pronunciation as word 使用本机 JavaScript 向 Google Cloud 文本转语音 API 进行身份验证 - Authenticate to Google Cloud text-to-speech API using native JavaScript 谷歌翻译 API 文字转语音:http 请求被禁止 - Google Translate API text-to-speech: http requests forbidden Firebase 具有用于文本到语音的谷歌应用程序凭据的云功能 API - Firebase cloud functions with google application credentials for text to speech API 如何在 Google Text to Speech 中调整发音音高 API - How to adjust Pronunciation Pitch in Google Text to Speech API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM