android studio google cloud speech api

> The audiofile is called "raw.wav" and is located under the resources directory
> Under the path "/raw/raw.wav"
> Credentials path is under "/raw/credentials.json"
>



~~~~build.gradle:
dependincies
{
	//...
    
    implementation 'com.google.cloud:google-cloud-speech:1.27.1'
    implementation 'io.grpc:grpc-okhttp:1.37.0'
    implementation 'io.grpc:grpc-protobuf:1.37.0'
    implementation 'io.grpc:grpc-stub:1.37.0'
    compileOnly 'org.apache.tomcat:annotations-api:6.0.53' // necessary for Java 9+
}

~~~~Code:

public class SpeechToText {
  	final int AUDIO_FILE_ID = R.raw.raw;
  final int CREDENTIALS_FILE_ID = R.raw.credentials;
	SpeechToText(Activity activity) throws Exception {

        SpeechClient speech = null;

        try {
            InputStream credentialsStream = activity.getResources().openRawResource(CREDENTIALS_FILE_ID);
            GoogleCredentials credentials = GoogleCredentials.fromStream(credentialsStream);
            FixedCredentialsProvider credentialsProvider = FixedCredentialsProvider.create(credentials);

            SpeechSettings speechSettings =
                    SpeechSettings.newBuilder()
                            .setCredentialsProvider(credentialsProvider)
                            .build();

             speech = SpeechClient.create(speechSettings);


            InputStream is = activity.getResources().openRawResource(AUDIO_FILE_ID);

            byte[] bytes = new byte[is.available()];
            if(is.read(bytes) == 0)
            {
                throw new Exception("Didn't Read bytes from resource");
            }

            ByteString audioBytes = ByteString.copyFrom(bytes);

            // 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 = speech.recognize(config, audio);
            List<SpeechRecognitionResult> results = response.getResultsList();

            for (SpeechRecognitionResult result : results) {
                List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList();
                for (SpeechRecognitionAlternative alternative : alternatives) {
                    Log.d("Transcription: ", alternative.getTranscript()); //answer
                }
            }
            speech.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source