17
17
package com .example .speech ;
18
18
19
19
import com .google .api .gax .grpc .OperationFuture ;
20
- import com .google .cloud .speech .spi .v1beta1 .SpeechClient ;
21
- import com .google .cloud .speech .v1beta1 . AsyncRecognizeResponse ;
22
- import com .google .cloud .speech .v1beta1 .RecognitionAudio ;
23
- import com .google .cloud .speech .v1beta1 .RecognitionConfig ;
24
- import com .google .cloud .speech .v1beta1 .RecognitionConfig .AudioEncoding ;
25
- import com .google .cloud .speech .v1beta1 .SpeechRecognitionAlternative ;
26
- import com .google .cloud .speech .v1beta1 .SpeechRecognitionResult ;
27
- import com .google .cloud .speech .v1beta1 . SyncRecognizeResponse ;
20
+ import com .google .cloud .speech .spi .v1 .SpeechClient ;
21
+ import com .google .cloud .speech .v1 . LongRunningRecognizeResponse ;
22
+ import com .google .cloud .speech .v1 .RecognitionAudio ;
23
+ import com .google .cloud .speech .v1 .RecognitionConfig ;
24
+ import com .google .cloud .speech .v1 .RecognitionConfig .AudioEncoding ;
25
+ import com .google .cloud .speech .v1 .SpeechRecognitionAlternative ;
26
+ import com .google .cloud .speech .v1 .SpeechRecognitionResult ;
27
+ import com .google .cloud .speech .v1 . RecognizeResponse ;
28
28
import com .google .protobuf .ByteString ;
29
29
30
30
import java .io .IOException ;
@@ -80,14 +80,15 @@ public static void syncRecognizeFile(String fileName) throws Exception, IOExcept
80
80
// Configure request with local raw PCM audio
81
81
RecognitionConfig config = RecognitionConfig .newBuilder ()
82
82
.setEncoding (AudioEncoding .LINEAR16 )
83
- .setSampleRate (16000 )
83
+ .setLanguageCode ("en-US" )
84
+ .setSampleRateHertz (16000 )
84
85
.build ();
85
86
RecognitionAudio audio = RecognitionAudio .newBuilder ()
86
87
.setContent (audioBytes )
87
88
.build ();
88
89
89
90
// Use blocking call to get audio transcript
90
- SyncRecognizeResponse response = speech .syncRecognize (config , audio );
91
+ RecognizeResponse response = speech .recognize (config , audio );
91
92
List <SpeechRecognitionResult > results = response .getResultsList ();
92
93
93
94
for (SpeechRecognitionResult result : results ) {
@@ -111,14 +112,15 @@ public static void syncRecognizeGcs(String gcsUri) throws Exception, IOException
111
112
// Builds the request for remote FLAC file
112
113
RecognitionConfig config = RecognitionConfig .newBuilder ()
113
114
.setEncoding (AudioEncoding .FLAC )
114
- .setSampleRate (16000 )
115
+ .setLanguageCode ("en-US" )
116
+ .setSampleRateHertz (16000 )
115
117
.build ();
116
118
RecognitionAudio audio = RecognitionAudio .newBuilder ()
117
119
.setUri (gcsUri )
118
120
.build ();
119
121
120
122
// Use blocking call for getting audio transcript
121
- SyncRecognizeResponse response = speech .syncRecognize (config , audio );
123
+ RecognizeResponse response = speech .recognize (config , audio );
122
124
List <SpeechRecognitionResult > results = response .getResultsList ();
123
125
124
126
for (SpeechRecognitionResult result : results ) {
@@ -130,6 +132,7 @@ public static void syncRecognizeGcs(String gcsUri) throws Exception, IOException
130
132
speech .close ();
131
133
}
132
134
135
+ /*
133
136
/**
134
137
* Performs non-blocking speech recognition on raw PCM audio and prints
135
138
* the transcription.
@@ -147,14 +150,15 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep
147
150
// Configure request with local raw PCM audio
148
151
RecognitionConfig config = RecognitionConfig .newBuilder ()
149
152
.setEncoding (AudioEncoding .LINEAR16 )
150
- .setSampleRate (16000 )
153
+ .setLanguageCode ("en-US" )
154
+ .setSampleRateHertz (16000 )
151
155
.build ();
152
156
RecognitionAudio audio = RecognitionAudio .newBuilder ()
153
157
.setContent (audioBytes )
154
158
.build ();
155
159
156
160
// Use non-blocking call for getting file transcription
157
- OperationFuture <AsyncRecognizeResponse > response = speech .asyncRecognizeAsync (config , audio );
161
+ OperationFuture <LongRunningRecognizeResponse > response = speech .longRunningRecognizeAsync (config , audio );
158
162
while (!response .isDone ()) {
159
163
System .out .println ("Waiting for response..." );
160
164
Thread .sleep (200 );
@@ -184,14 +188,15 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio
184
188
// Configure remote file request for FLAC file
185
189
RecognitionConfig config = RecognitionConfig .newBuilder ()
186
190
.setEncoding (AudioEncoding .FLAC )
187
- .setSampleRate (16000 )
191
+ .setLanguageCode ("en-US" )
192
+ .setSampleRateHertz (16000 )
188
193
.build ();
189
194
RecognitionAudio audio = RecognitionAudio .newBuilder ()
190
195
.setUri (gcsUri )
191
196
.build ();
192
197
193
198
// Use non-blocking call for getting file transcription
194
- OperationFuture <AsyncRecognizeResponse > response = speech .asyncRecognizeAsync (config , audio );
199
+ OperationFuture <LongRunningRecognizeResponse > response = speech .longRunningRecognizeAsync (config , audio );
195
200
while (!response .isDone ()) {
196
201
System .out .println ("Waiting for response..." );
197
202
Thread .sleep (200 );
0 commit comments