Flutter:Google Speech-To-Text API 总是 returns null
Flutter: Google Speech-To-Text API always returns null
我正在尝试调用 google 语音转文本 api 但它总是 return 我的结果为空。我从这个答案中得到了实现提示:
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) 中列出的不同编码方法,但仍然不成功。
问题出在录音机库上。解决问题的记录器:
https://pub.dev/packages/flutter_audio_recorder
我最近 运行 也遇到了这个确切的问题,我认为问题在于文件的编码。我正在为 flutter_sound 使用 v2.0.3,录制后的默认文件类型是 aac,但是,根据 https://cloud.google.com/speech-to-text/docs/encoding,它们唯一可接受的文件类型是 flac、amr、wav 和其他一些。
我使用的是https://pub.dev/packages/google_speech,预设编码是
'encoding' : 'LINEAR16',
这解释了为什么 wav 文件有效
我正在尝试调用 google 语音转文本 api 但它总是 return 我的结果为空。我从这个答案中得到了实现提示: 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) 中列出的不同编码方法,但仍然不成功。
问题出在录音机库上。解决问题的记录器: https://pub.dev/packages/flutter_audio_recorder
我最近 运行 也遇到了这个确切的问题,我认为问题在于文件的编码。我正在为 flutter_sound 使用 v2.0.3,录制后的默认文件类型是 aac,但是,根据 https://cloud.google.com/speech-to-text/docs/encoding,它们唯一可接受的文件类型是 flac、amr、wav 和其他一些。
我使用的是https://pub.dev/packages/google_speech,预设编码是
'encoding' : 'LINEAR16',
这解释了为什么 wav 文件有效