Android flatter:googlespeech-To-Text API始终返回null
我试图调用google speech to text api,但它总是返回空结果。我从这个答案中得到了实现提示: 我正在使用flatter_sound()包来录制音频,然后将base64编码的音频发送到语音API 录音代码Android flatter:googlespeech-To-Text API始终返回null,android,flutter,dart,speech-to-text,google-cloud-speech,Android,Flutter,Dart,Speech To Text,Google Cloud Speech,我试图调用google speech to text api,但它总是返回空结果。我从这个答案中得到了实现提示: 我正在使用flatter_sound()包来录制音频,然后将base64编码的音频发送到语音API 录音代码 String path = await flutterSound.startRecorder( Platform.isIOS ? 'ios.' : 'android.aac', androidEncoder: AndroidEncoder.A
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;
}
final\u credentials=新的ServiceAccountCredentials.fromJson(r''
{
“类型”:“服务账户”,
“项目id”:“,
“私钥id”:“,
....
''');
final _SCOPES=const[SpeechApi.CloudPlatformScope];
void convert()异步{
clientViaServiceAccount(\u凭据,\u作用域)。然后((http\u客户端){
var SpeechApi=新SpeechApi
试一试{
字符串myPath=\u路径;
_readFileByte(myPath).then((bytesData)异步{
String audioString=base64.encode(字节数据);
打印('audioString:$audioString');
字符串audioStringSample=“”;
RecognizeRequest r=RecognizeRequest();
RecognitionAudio=RecognitionAudio.fromJson({'content':audioString});
r、 音频=音频;
RecognitionConfig=RecognitionConfig.fromJson({
“语言代码”:“en US”,
“编码”:“LINEAR16”,
“赫兹”:16000,
});
r、 config=config;
speech.speech.recognize(r).then((结果){
for(结果中的var结果。结果){
打印(结果。备选方案[0]。成绩单);
}
});
});
}捕获(e){
//如果路径无效或无法读取
印刷品(e);
}
});
}
Future\u readFileByte(字符串文件路径)异步{
Uri myUri=Uri.parse(filePath);
File audioFile=File.fromUri(myUri);
Uint8List字节;
等待audioFile.readAsBytes()。然后((值){
字节=Uint8List.fromList(值);
打印(“字节读取完成”);
}).catchError((onError){
打印('从路径读取音频时出现异常错误:'+
onError.toString());
});
返回字节;
}
上述代码适用于audioStringSample
(在此处查找示例音频内容:),但当我传递自己的音频时,即audioString
,结果总是空的。我在这里做错了什么
注:我还尝试了语音API参考()中列出的不同编码方法,但仍然没有成功。问题出在记录器库中。解决问题的记录器:
我最近也遇到了这个确切的问题,我认为问题在于文件的编码。我使用v2.0.3处理颤振声音,录制后的默认文件类型是aac,但是,根据他们的说法,唯一可接受的文件类型是flac、amr、wav和其他一些文件 我正在使用,预设编码是 “编码”:“LINEAR16”
这解释了wav文件工作的原因您确定音频数据正确吗?一种测试方法:如果您录制大约2秒,字节数据的大小(以千字节为单位)(检查长度)?我知道这是6个月大的问题,但我最近做了一件类似的事情,如果可能的话,我想帮助你。如果这个问题已经解决了,也许你可以把答案放在这里给其他人。干杯!这是通过使用另一个录音库解决的。我也会发布答案。我尝试了不同的编码方法,但没有一种有效。我最终决定改变t他在图书馆工作。这是一个已经有一年历史的问题了,不知道当前的API情况