0

I am trying to do some work using the Google Speech API. How can I specify the authentication keys using the Google Speech API Java library? I am using this method Google Speech API credentials

I want to convert audio file into text

Thank you

CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream("home/hussain/AndroidStudioProjects/NestedLogics/RouteApplication/service-account.json")));

        SpeechSettings settings = SpeechSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();
        //SpeechClient speechClient = SpeechClient.create(settings);
        SpeechClient speech = SpeechClient.create(settings);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Path path = Paths.get(file_path);
            byte[] data = Files.readAllBytes(path);
            ByteString audioBytes = ByteString.copyFrom(data);
            RecognitionConfig config = RecognitionConfig.newBuilder()
                    .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
                    .setSampleRateHertz(16000)
                    .setLanguageCode("en-US")
                    .build();
            RecognitionAudio audio = RecognitionAudio.newBuilder()
                    .setContent(audioBytes)
                    .build();

            RecognizeResponse response = speech.recognize(config,audio);
            List<SpeechRecognitionResult> results = response.getResultsList();
            for (SpeechRecognitionResult result: results)
            {
                SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
                Log.d("HOME", "convertAudioToSpeech: "+alternative.getTranscript());
            }
        }

build.gradle

dependencies {
   implementation fileTree(include: ['*.jar'], dir: 'libs')
   implementation 'com.android.support:appcompat-v7:28.0.0'
   implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   implementation 'com.android.support:support-v4:28.0.0'
   implementation 'com.android.support:design:28.0.0'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
   implementation('com.mapbox.mapboxsdk:mapbox-android-sdk:6.6.1') {
       transitive = true
   }
   implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-locationlayer:0.10.0'
   implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-localization:0.5.0'
   implementation 'com.mapbox.mapboxsdk:mapbox-android-core:0.2.1'
   //firebase
   implementation 'com.google.firebase:firebase-core:16.0.4'
   implementation 'com.google.firebase:firebase-auth:16.0.4'
   implementation 'com.google.firebase:firebase-database:16.0.4'
   implementation 'com.writingminds:FFmpegAndroid:0.3.2'
   implementation 'com.karumi:dexter:4.2.0' // this is use for runtime permissions
   implementation 'com.google.cloud:google-cloud-speech:0.30.0-alpha'
   annotationProcessor 'com.google.cloud:google-cloud-speech:0.30.0-alpha'
   implementation 'com.android.support:multidex:1.0.3'
 }
2
  • Are you using the Google Cloud SDK? Commented Nov 1, 2018 at 20:26
  • yes @ItamarKerbel Commented Nov 5, 2018 at 5:12

2 Answers 2

1

After you generated a JSON file with credentials, put it under app/src/main/res/raw/credential.json in the Android project sources. And then in the speech service creating code:

InputStream stream = getContext().getResources().openRawResource(R.raw.credential);                   
SpeechSettings settings =
  SpeechSettings.newBuilder().setCredentialsProvider(
    new CredentialsProvider() {
      @Override
      public Credentials getCredentials() throws IOException {
        return GoogleCredentials.fromStream(stream);
      }
    }
  ).build();
Sign up to request clarification or add additional context in comments.

5 Comments

i tried this but i got an error Caused by: java.lang.NoSuchMethodError: No direct method <init>(Lcom/google/api/gax/rpc/ClientContext;)V in class Lcom/google/api/gax/rpc/ClientSettings$Builder; or its super classes (declaration of 'com.google.api.gax.rpc.ClientSettings$Builder' appears in /data/app/com.example.hussain.routeapplication-BtQ8fKEs7dp_ENQO4aatuw==/split_lib_dependencies_apk.apk) at com.google.cloud.speech.v1.SpeechSettings$Builder.<init>(SpeechSettings.java:259)
It's hard to say just from this error message. Highly likely, it's a problem with dependencies, so check that your gradle configs look exactly like in the sample application.
Or may be you're using an outdated version of google-cloud-speech. Mine is implementation 'com.google.cloud:google-cloud-speech:0.41.0-alpha'
Alexander.. build.gradle is up there.
@hussainabbas I recommend to change the dependency version from 0.30.0 to 0.41.0-alpha
0

I don't have much experience with the Google Cloud SDK but from what I understand you should:

Create the project under the Google Cloud Services.

After you create the project and set up a payment method (as this service is not free) you will be able to download a private key in the form of a JSON file.

You then need to set up an environment variable name GOOGLE_APPLICATION_CREDENTIALS on your machine to point to that file. Then you should be ready to go.

You can read a detailed step by step how to here.

Click on the blue button and continue from there...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.