我正在尝试调用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
时,结果始终为null。我在这里做错什么了吗?附注:我还尝试了语音编码引用(https://cloud.google.com/speech-to-text/docs/encoding)中列出的各种编码方法,但仍未成功。
最佳答案
问题在于记录器库。解决该问题的记录器:
https://pub.dev/packages/flutter_audio_recorder
关于android - Flutter:Google Speech-To-Text API始终返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59324929/