I setup a demo of the Google Cloud Speech API running on Android with AudioRecord to retrieve audio from the microphone.
Its working but after a while (+/- 1 minute) the channel closes by itself.
I basically used the java StreamRecognizeClient example, including the last changes to the gRPC 1.0 and the ManagedChannelBuilder
There is a definition to set a timeout or am I doing something wrong here?
Here is the main code setup:
Setup channel
privatestaticfinalList<String> OAUTH2_SCOPES = Arrays.asList("https://www.googleapis.com/auth/cloud-platform");
publicstaticManagedChannelcreateChannel(InputStreamauthorizationFile, Stringhost, intport) throwsIOException {
GoogleCredentialscreds = GoogleCredentials.fromStream(authorizationFile);
creds = creds.createScoped(OAUTH2_SCOPES);
ManagedChannelchannel =
ManagedChannelBuilder.forAddress(host, port)
.intercept(newClientAuthInterceptor(creds, Executors.newSingleThreadExecutor()))
.build();
returnchannel;
}Recognize setup and loop
publicvoidrecognize() throwsInterruptedException, IOException {
try {
// Build and send a StreamingRecognizeRequest containing the parameters for// processing the audio.RecognitionConfigconfig =
RecognitionConfig.newBuilder()
.setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
.setSampleRate(this.RECORDER_SAMPLERATE)
//.setLanguageCode("en-US")
.build();
// Sreaming configStreamingRecognitionConfigstreamingConfig =
StreamingRecognitionConfig.newBuilder()
.setConfig(config)
.setInterimResults(true)
.setSingleUtterance(false)
.build();
// First requestStreamingRecognizeRequestinitial =
StreamingRecognizeRequest.newBuilder().setStreamingConfig(streamingConfig).build();
requestObserver.onNext(initial);
// Microphone listener and recorderrecorder = newAudioRecord(MediaRecorder.AudioSource.MIC,
this.RECORDER_SAMPLERATE,
this.RECORDER_CHANNELS,
this.RECORDER_AUDIO_ENCODING,
bufferSize);
recorder.startRecording();
byte[] buffer = newbyte[bufferSize];
intrecordState;
// loop through the audio samplingswhile ( (recordState = recorder.read(buffer, 0, buffer.length) ) > -1 ) {
// skip if there is no dataif( recordState < 0 )
continue;
// create a new recognition requestStreamingRecognizeRequestrequest =
StreamingRecognizeRequest.newBuilder()
.setAudioContent(ByteString.copyFrom(buffer, 0, buffer.length))
.build();
// put it on the worksrequestObserver.onNext(request);
}
} catch (RuntimeExceptione) {
// Cancel RPC.requestObserver.onError(e);
throwe;
}
// Mark the end of requests.requestObserver.onCompleted();
}The full code is in this repo: https://github.com/Cloudoki/android-google-cloud-speech-api
Need some help here, because I'm not figuring it out!
Thanks
I setup a demo of the Google Cloud Speech API running on Android with AudioRecord to retrieve audio from the microphone.
Its working but after a while (+/- 1 minute) the channel closes by itself.
I basically used the java StreamRecognizeClient example, including the last changes to the gRPC 1.0 and the ManagedChannelBuilder
There is a definition to set a timeout or am I doing something wrong here?
Here is the main code setup:
Setup channel
Recognize setup and loop
The full code is in this repo: https://github.com/Cloudoki/android-google-cloud-speech-api
Need some help here, because I'm not figuring it out!
Thanks