I am trying to use speech-to-text API of Google Cloud Platform for my Android App. I have passed it a recorded audio file for conversion to text. I can't solve an IOException which is described as "The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information."
I've created a service account, enabled the particular API, created a account key(cred.json). Set the environment variable "GOOGLE_APPLICATION_CREDENTIALS" to the [path] of the file(cred.json from above). This was done in .bash_profile file on mac.
Here is the problem: When I check for the env. variable from terminal using
echo $GOOGLE_APPLICATION_CREDENTIALS
the result is [path] of the cred.json file
But while deubgging the app if I try to check "GOOGLE_APPLICATION_CREDENTIALS" it shows null. I checked it using
Log.d("@sttenv", System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
This is the reason why I get the IOException mentioned above. This line throws the IOException.
SpeechClient speechClient = SpeechClient.create();
The SpeechClient which is the very start of the code itself gives an IOException.
try {
SpeechClient speechClient = SpeechClient.create();
// Reads the audio file into memory
Path path = Paths.get(tempFileName);
byte[] data = Files.readAllBytes(path);
ByteString audioBytes = ByteString.copyFrom(data);
// Builds the sync recognize request
RecognitionConfig config = RecognitionConfig.newBuilder()
.setEncoding(AudioEncoding.LINEAR16)
.setSampleRateHertz(44100)
.setLanguageCode("en-US")
.build();
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setContent(audioBytes)
.build();
// Performs speech recognition on the audio file
RecognizeResponse response = speechClient.recognize(config, audio);
List<SpeechRecognitionResult> results = response.getResultsList();
for (SpeechRecognitionResult result : results) {
// There can be several alternative transcripts for a given chunk of speech. Just use the
// first (most likely) one here.
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
//System.out.printf("Transcription: %s%n", alternative.getTranscript());
log.debug("Transcription: %s%n", alternative.getTranscript());
}
}
catch (IOException e){
e.printStackTrace();
}
How should I tackle this? I tried setting the env. variable from the terminal, no use. Also I wanted to know, if we install this app in a android phone(not on emulator), does it require the cred.json file to be present in the phone itself? Because the cred.josn (account key) is on my mac. And I'm trying to access the API through a Android phone? So should I save the cred.json on my phone and give it's path to the env. variable?