How to use Google Cloud Speech v1 on Android?

1.1k Views Asked by At

I recently updated my Android Google Cloud Speech code from v1beta1 to v1. There were a couple of changes in the API, one of them was a new method called getWordsList().

I want to use the getWordsList() in my Android project, however the method doesn't seem to be visible to my code:

import com.google.cloud.speech.v1.SpeechGrpc;
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
import com.google.cloud.speech.v1.StreamingRecognizeResponse;
import com.google.cloud.speech.v1.WordInfo;

...

public void onNext(StreamingRecognizeResponse response) {
  int numOfResults = response.getResultsCount();
  if( numOfResults > 0 ){
    for (int i=0;i<numOfResults;i++){
      StreamingRecognitionResult result = response.getResultsList().get(i);
      SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
      for (WordInfo wordInfo: alternative.getWordsList()) { //-->>Cannot resolve 'method'
        System.out.println(wordInfo.getWord());
        System.out.println(wordInfo.getStartTime().getSeconds() + " ");
      }
      ...

This code is from the official repo, however, I am getting the following error:

Cannot resolve method 'getWordsList()'

Here is my gradle:

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "org.test.test"
        minSdkVersion 24
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        // Enabling multidex support.
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.0.0'
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.0.0'
        }
        javalite {
            artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
        }
    }
    generateProtoTasks {
        all().each {
            task ->
                task.builtins {
                    remove javanano
                    java {
                    }
                }
                task.plugins {
                    grpc {
                    }
                }
        }
    }
}

ext {
    supportLibraryVersion = '25.0.0'
    grpcVersion = '1.4.0'
}

dependencies {
    // Generic dependencies
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.google.cloud:google-cloud-speech:0.23.1-alpha'

    // Support libraries
    compile "com.android.support:appcompat-v7:$supportLibraryVersion"
    compile "com.android.support:design:$supportLibraryVersion"
    compile "com.android.support:cardview-v7:$supportLibraryVersion"
    compile "com.android.support:recyclerview-v7:$supportLibraryVersion"
    compile 'com.android.support:multidex:1.0.1'

    // gRPC
    compile 'javax.annotation:javax.annotation-api:1.2'
    compile("io.grpc:grpc-protobuf:${grpcVersion}") {
        exclude module: 'jsr305'
    }
    compile("io.grpc:grpc-stub:${grpcVersion}") {
        exclude module: 'jsr305'
    }
    compile("io.grpc:grpc-auth:${grpcVersion}") {
        exclude module: 'jsr305'
    }
    compile("io.grpc:grpc-okhttp:${grpcVersion}") {
        exclude module: 'jsr305'
    }
    // OAuth2 for Google API
    compile('com.google.auth:google-auth-library-oauth2-http:0.3.0') {
        exclude module: 'jsr305'
        exclude module: 'httpclient'
    }

}

I also noticed, I cannot use all grpc libraries, for example the following library cannot be imported:

import com.google.api.gax.rpc.StreamingCallable;

How can I use getWordsList() correctly in Android? Am I not using the correct build version?

2

There are 2 best solutions below

1
On

I use this instead of getWordList():

final SpeechRecognitionAlternative alternative = result.getAlternatives(0);
text = alternative.getTranscript();

Hope it works.

0
On

You did not enclosed the code you use for the request, but make sure you are adding setEnableWordTimeOffsets to the RecognizeRequest :

  public void recognizeInputStream(InputStream stream) {
    try {
        mApi.recognize(
                RecognizeRequest.newBuilder()
                        .setConfig(RecognitionConfig.newBuilder()
                                .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
                                .setLanguageCode("en-US")
                                .setSampleRateHertz(16000)
                                .setEnableWordTimeOffsets(true)
                                .build())
                        .setAudio(RecognitionAudio.newBuilder()
                                .setContent(ByteString.readFrom(stream))
                                .build())
                        .build(),
                mFileResponseObserver);
    } catch (IOException e) {
        Log.e(TAG, "Error loading the input", e);
    }
}