Flutter:Google Speech-To-Text API始终返回null

时间:2019-12-13 14:52:47

标签: android flutter dart speech-to-text google-cloud-speech

我正在尝试调用Google语音文本API,但它始终返回空结果。我从以下答案中获得了实现提示: Using gcloud speech api for real-time speech recognition in dart, flutter

我正在使用flutter_sound(https://pub.dev/packages/flutter_sound)包来录制音频,然后将base64编码的音频发送到语音API

录制音频的代码

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

从上述代码成功生成了扩展名为.aac的音频文件android.aac。

下面的代码用于将音频数据发送到语音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;
  }

以上代码与audioStringSample完美配合(在此处查找示例音频内容:https://gist.github.com/DazWilkin/34d628b998b4266be818ffb3efd688aa),但是当我传递自己的音频即audioString时,结果始终为空。我在这里做错什么了吗?

P.S:我还尝试了语音API参考(https://cloud.google.com/speech-to-text/docs/encoding)中列出的各种编码方法,但仍未成功。

2 个答案:

答案 0 :(得分:1)

问题在于记录器库中。解决该问题的记录器: https://pub.dev/packages/flutter_audio_recorder

答案 1 :(得分:0)

我最近也遇到了这个确切的问题,我认为问题在于文件的编码。我正在为flutter_sound使用v2.0.3,并且录制后的默认文件类型为aac,但是,根据https://cloud.google.com/speech-to-text/docs/encoding,它们仅接受flac,amr,wav等文件类型。

我使用的是https://pub.dev/packages/google_speech,预设编码是

“编码”:“ LINEAR16”,

解释了为什么wav文件起作用