[英]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.