I want to use google cloud speech api in and android app for which I am following this link provided by google https://cloud.google.com/speech-to-text/docs/libraries#client-libraries-install-java
I have tried the solution provided in link https://developers.google.com/accounts/docs/application-default-credentials but it is still not working.
Windows Environment Variable user and system both have GOOGLE_APPLICATION_CREDENTIALS=C:\Users\ANSH\Downloads\credential.json
All the necessary imports are present.
private void recognizeSpeech(){
try{
SpeechClient speechClient = SpeechClient.create();
String languageCode="en-US";
int sampleRateHertz=16000;
RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.LINEAR16;
RecognitionConfig config =
RecognitionConfig.newBuilder()
.setLanguageCode(languageCode)
.setSampleRateHertz(sampleRateHertz)
.setEncoding(encoding)
.build();
Path path = Paths.get(fileName);
byte[] data = Files.readAllBytes(path);
ByteString content = ByteString.copyFrom(data);
RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(content).build();
RecognizeRequest request =
RecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build();
RecognizeResponse response = speechClient.recognize(request);
for (SpeechRecognitionResult result : response.getResultsList()) {
// First alternative is the most probable result
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
outputText.setText(alternative.getTranscript());
}
}
catch (Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
credential.json contains the key downloaded using the service account
https://cloud.google.com/speech-to-text/docs/libraries mentions a note.
Note: Cloud Java client libraries do not currently support Android.
Does it mean it can not be used in android studio? If yes how to use it?
You might want to check this github link to see how the exception was resolved, but please consider that the Speech To Text Client Libraries specifies that doesn't support Android, as it was posted in the comments.
In addition, as your question is about an issue when setting authentication in your Android App, I would recommend you use an OAuth 2.0 client ID. You will need to generate an OAuth 2.0 client ID from the Google Console and then follow Android Instructions for passing requestIdToken
or requestServerAuthCode
in the GoogleSignInOptions
. Also, please keep in mind that this doesn't guarantee the correct integration between Speech API and Android, so if you get this work please share it with us.