android studio google cloud speech api

Solutions on MaxInterview for android studio google cloud speech api by the best coders in the world

showing results for - "android studio google cloud speech api"
Paulina
25 Apr 2017
1> The audiofile is called "raw.wav" and is located under the resources directory
2> Under the path "/raw/raw.wav"
3> Credentials path is under "/raw/credentials.json"
4>
5
6
7
8~~~~build.gradle:
9dependincies
10{
11	//...
12    
13    implementation 'com.google.cloud:google-cloud-speech:1.27.1'
14    implementation 'io.grpc:grpc-okhttp:1.37.0'
15    implementation 'io.grpc:grpc-protobuf:1.37.0'
16    implementation 'io.grpc:grpc-stub:1.37.0'
17    compileOnly 'org.apache.tomcat:annotations-api:6.0.53' // necessary for Java 9+
18}
19
20~~~~Code:
21
22public class SpeechToText {
23  	final int AUDIO_FILE_ID = R.raw.raw;
24  final int CREDENTIALS_FILE_ID = R.raw.credentials;
25	SpeechToText(Activity activity) throws Exception {
26
27        SpeechClient speech = null;
28
29        try {
30            InputStream credentialsStream = activity.getResources().openRawResource(CREDENTIALS_FILE_ID);
31            GoogleCredentials credentials = GoogleCredentials.fromStream(credentialsStream);
32            FixedCredentialsProvider credentialsProvider = FixedCredentialsProvider.create(credentials);
33
34            SpeechSettings speechSettings =
35                    SpeechSettings.newBuilder()
36                            .setCredentialsProvider(credentialsProvider)
37                            .build();
38
39             speech = SpeechClient.create(speechSettings);
40
41
42            InputStream is = activity.getResources().openRawResource(AUDIO_FILE_ID);
43
44            byte[] bytes = new byte[is.available()];
45            if(is.read(bytes) == 0)
46            {
47                throw new Exception("Didn't Read bytes from resource");
48            }
49
50            ByteString audioBytes = ByteString.copyFrom(bytes);
51
52            // Builds the sync recognize request
53            RecognitionConfig config =
54                    RecognitionConfig.newBuilder()
55                            .setEncoding(AudioEncoding.LINEAR16)
56                            .setSampleRateHertz(44100)
57                            .setLanguageCode("en-US")
58                            .build();
59            RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(audioBytes).build();
60
61            // Performs speech recognition on the audio file
62            RecognizeResponse response = speech.recognize(config, audio);
63            List<SpeechRecognitionResult> results = response.getResultsList();
64
65            for (SpeechRecognitionResult result : results) {
66                List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList();
67                for (SpeechRecognitionAlternative alternative : alternatives) {
68                    Log.d("Transcription: ", alternative.getTranscript()); //answer
69                }
70            }
71            speech.close();
72        } catch (IOException e) {
73
74            e.printStackTrace();
75        }
76    }
77}