48
48
import org .apache .commons .cli .Options ;
49
49
import org .apache .commons .cli .ParseException ;
50
50
51
+ import java .net .URI ;
51
52
import java .io .IOException ;
52
53
import java .nio .file .Files ;
53
54
import java .nio .file .Path ;
@@ -72,7 +73,7 @@ public class NonStreamingRecognizeClient {
72
73
73
74
private final String host ;
74
75
private final int port ;
75
- private final String file ;
76
+ private final URI input ;
76
77
private final int samplingRate ;
77
78
78
79
private final ManagedChannel channel ;
@@ -81,11 +82,11 @@ public class NonStreamingRecognizeClient {
81
82
/**
82
83
* Construct client connecting to Cloud Speech server at {@code host:port}.
83
84
*/
84
- public NonStreamingRecognizeClient (String host , int port , String file , int samplingRate )
85
+ public NonStreamingRecognizeClient (String host , int port , URI input , int samplingRate )
85
86
throws IOException {
86
87
this .host = host ;
87
88
this .port = port ;
88
- this .file = file ;
89
+ this .input = input ;
89
90
this .samplingRate = samplingRate ;
90
91
91
92
GoogleCredentials creds = GoogleCredentials .getApplicationDefault ();
@@ -99,10 +100,7 @@ public NonStreamingRecognizeClient(String host, int port, String file, int sampl
99
100
}
100
101
101
102
private AudioRequest createAudioRequest () throws IOException {
102
- Path path = Paths .get (file );
103
- return AudioRequest .newBuilder ()
104
- .setContent (ByteString .copyFrom (Files .readAllBytes (path )))
105
- .build ();
103
+ return AudioRequestFactory .createRequest (this .input );
106
104
}
107
105
108
106
public void shutdown () throws InterruptedException {
@@ -115,10 +113,10 @@ public void recognize() {
115
113
try {
116
114
audio = createAudioRequest ();
117
115
} catch (IOException e ) {
118
- logger .log (Level .WARNING , "Failed to read audio file : " + file );
116
+ logger .log (Level .WARNING , "Failed to read audio uri input : " + input );
119
117
return ;
120
118
}
121
- logger .info ("Sending " + audio .getContent ().size () + " bytes from audio file : " + file );
119
+ logger .info ("Sending " + audio .getContent ().size () + " bytes from audio uri input : " + input );
122
120
InitialRecognizeRequest initial = InitialRecognizeRequest .newBuilder ()
123
121
.setEncoding (AudioEncoding .LINEAR16 )
124
122
.setSampleRate (samplingRate )
@@ -147,8 +145,8 @@ public static void main(String[] args) throws Exception {
147
145
CommandLineParser parser = new DefaultParser ();
148
146
149
147
Options options = new Options ();
150
- options .addOption (OptionBuilder .withLongOpt ("file " )
151
- .withDescription ("path to audio file " )
148
+ options .addOption (OptionBuilder .withLongOpt ("uri " )
149
+ .withDescription ("path to audio uri " )
152
150
.hasArg ()
153
151
.withArgName ("FILE_PATH" )
154
152
.create ());
@@ -170,10 +168,10 @@ public static void main(String[] args) throws Exception {
170
168
171
169
try {
172
170
CommandLine line = parser .parse (options , args );
173
- if (line .hasOption ("file " )) {
174
- audioFile = line .getOptionValue ("file " );
171
+ if (line .hasOption ("uri " )) {
172
+ audioFile = line .getOptionValue ("uri " );
175
173
} else {
176
- System .err .println ("An Audio file path must be specified (e.g. /foo/baz.raw)." );
174
+ System .err .println ("An Audio uri must be specified (e.g. file:/ /foo/baz.raw)." );
177
175
System .exit (1 );
178
176
}
179
177
@@ -203,7 +201,7 @@ public static void main(String[] args) throws Exception {
203
201
}
204
202
205
203
NonStreamingRecognizeClient client =
206
- new NonStreamingRecognizeClient (host , port , audioFile , sampling );
204
+ new NonStreamingRecognizeClient (host , port , URI . create ( audioFile ) , sampling );
207
205
try {
208
206
client .recognize ();
209
207
} finally {
0 commit comments