Flutter: Google Speech-To-Text API always returns null
The problem lied in the recorder library. The recorder which resolved the problem: https://pub.dev/packages/flutter_audio_recorder
Hamza Ahmed Khan
Updated on December 16, 2022Comments
-
Hamza Ahmed Khan over 1 year
I'm trying to call google speech-to-text api but it always return me null result. I got the implementation hint from this answer: 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
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.
Below code is used for sending audio data to speech 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 i.eaudioString
the result is always null. Anything I am doing wrong here?P.S: 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.
-
riper almost 4 yearsAre you sure the audio data is correct? One way to test: If you do a recording of about 2 seconds, how big is bytesData in kilobytes (check the length of it)? I know this is 6 months old but I did a similar thing recently and wanted to help if possible. If this is already solved, maybe you could put the answer here for others. Cheers!
-
Hamza Ahmed Khan almost 4 yearsIt was solved by using another audio recording library. I'll also post the answer
-
-
Hamza Ahmed Khan over 3 yearsI tried different encoding methods but none of them worked. I finally decided to change the library which worked. It's a year-old issue now, don't know about the current API situation