@@ -40,9 +40,8 @@ public final class AndroidSpeechRecognizer implements ConversationalFlowComponen
4040
4141 // Resources
4242 private final Application application ;
43- private final ComponentConfig config ;
4443 private final AndroidHandler mainHandler ;
45- private final AndroidAudioHandler audioHandler ;
44+ private final AndroidAudioManager audioManager ;
4645 private final BluetoothSco bluetoothSco ;
4746 private final Intent speechRecognizerIntent ;
4847 private final SpeechRecognizerCreator <android .speech .SpeechRecognizer > recognizerCreator ;
@@ -101,7 +100,7 @@ private void cleanup() {
101100 @ Override
102101 public void reset () {
103102 releaseTimeout ();
104- audioHandler .abandonAudioFocus ();
103+ audioManager .abandonAudioFocus ();
105104 cleanup ();
106105 super .setTryAgain (false );
107106 setOnError (null );
@@ -115,7 +114,7 @@ public void reset() {
115114 @ Override
116115 public void onReadyForSpeech (Bundle params ) {
117116 if (!bluetoothSco .isBluetoothScoOn ()) {
118- audioHandler .requestAudioFocus (config .isAudioExclusiveRequiredForRecognizer ());
117+ audioManager .requestAudioFocus (configuration .isAudioExclusiveRequiredForRecognizer ());
119118 }
120119 //resetVolumeForBeep();
121120 super .onReadyForSpeech (params );
@@ -215,15 +214,16 @@ private boolean needRetry(int error) {
215214 }
216215 };
217216
218- // Log stuff
219- private ILogger logger ;
220-
221- AndroidSpeechRecognizer (Application application , ComponentConfig configuration ,
222- AndroidAudioHandler audioHandler , BluetoothSco bluetoothSco ,
223- SpeechRecognizerCreator recognizerCreator , ILogger logger ) {
217+ AndroidSpeechRecognizer (Application application ,
218+ ComponentConfig configuration ,
219+ AndroidAudioManager audioManager ,
220+ BluetoothSco bluetoothSco ,
221+ SpeechRecognizerCreator <android .speech .SpeechRecognizer >
222+ recognizerCreator ,
223+ ILogger logger ) {
224224 this .application = application ;
225- this .config = configuration ;
226- this .audioHandler = audioHandler ;
225+ this .configuration = configuration ;
226+ this .audioManager = audioManager ;
227227 this .bluetoothSco = bluetoothSco ;
228228 this .logger = logger ;
229229 this .executorService = Executors .newSingleThreadExecutor ();
@@ -237,6 +237,75 @@ private boolean needRetry(int error) {
237237 this .recognizerCreator = recognizerCreator ;
238238 }
239239
240+ private void startListening () {
241+ executorService .submit (() -> {
242+ lock .lock ();
243+ try {
244+ mainHandler .post (() -> {
245+ if (speechRecognizer == null ) {
246+ speechRecognizer = recognizerCreator .create ();
247+ logger .v (TAG , "ANDROID VOICE - created" );
248+ }
249+ recognitionListener .startTimeout ();
250+ recognitionListener .setRmsDebug (rmsDebug );
251+ if (noSoundThreshold > 0 ) recognitionListener .setNoSoundThreshold (noSoundThreshold );
252+ if (lowSoundThreshold > 0 ) recognitionListener .setLowSoundThreshold (lowSoundThreshold );
253+ logger .i (TAG , "ANDROID VOICE - start listening" );
254+ speechRecognizer .setRecognitionListener (recognitionListener );
255+ //adjustVolumeForBeep();
256+ speechRecognizer .startListening (speechRecognizerIntent );
257+ executorService .submit (lock ::unlock );
258+ });
259+ } catch (Exception e ) {
260+ logger .logException (e );
261+ lock .unlock ();
262+ }
263+ });
264+ }
265+
266+ private void handleListeners (RecognizerListener ... listeners ) {
267+ recognitionListener .reset ();
268+ if (listeners != null && listeners .length > 0 ) {
269+ for (RecognizerListener item : listeners ) {
270+ if (item instanceof OnRecognizerReady ) {
271+ recognitionListener .setOnReady ((OnRecognizerReady ) item );
272+ }
273+ else if (item instanceof OnRecognizerResults ) {
274+ recognitionListener .setOnResults ((OnRecognizerResults ) item );
275+ }
276+ else if (item instanceof OnRecognizerMostConfidentResult ) {
277+ recognitionListener .setOnMostConfidentResult ((OnRecognizerMostConfidentResult ) item );
278+ }
279+ else if (item instanceof OnRecognizerPartialResults ) {
280+ recognitionListener .setOnPartialResults ((OnRecognizerPartialResults ) item );
281+ }
282+ else if (item instanceof OnRecognizerError ) {
283+ recognitionListener .setOnError ((OnRecognizerError ) item );
284+ }
285+ }
286+ }
287+ }
288+
289+ public void setTryAgain (boolean tryAgain ) {
290+ this .recognitionListener .setTryAgain (tryAgain );
291+ }
292+
293+ public void setPartialResults (boolean partial ) {
294+ this .speechRecognizerIntent .putExtra (RecognizerIntent .EXTRA_PARTIAL_RESULTS , partial );
295+ }
296+
297+ public void setRmsDebug (boolean rmsDebug ) {
298+ this .rmsDebug = rmsDebug ;
299+ }
300+
301+ public void setNoSoundThreshold (float maxValue ) {
302+ this .noSoundThreshold = maxValue ;
303+ }
304+
305+ public void setLowSoundThreshold (float maxValue ) {
306+ this .lowSoundThreshold = maxValue ;
307+ }
308+
240309 @ Override
241310 public void release () {
242311 if (mainHandler != null ) {
@@ -313,110 +382,6 @@ public void shutdown() {
313382 });
314383 }
315384
316- @ Override
317- public void listen (RecognizerListener ... listeners ) {
318- logger .i (TAG , "ANDROID VOICE - start listening" );
319- handleListeners (listeners );
320- // Check whether Sco is connected or required
321- logger .i (TAG , "ANDROID VOICE - is bluetooth Sco required: " +
322- Boolean .toString (config .isBluetoothScoRequired ()));
323- if (config .isBluetoothScoRequired () && !bluetoothSco .isBluetoothScoOn ()) {
324- // Sco Listener
325- BluetoothScoListener listener = new BluetoothScoListener () {
326- @ Override
327- public void onConnected () {
328- logger .w (TAG , "ANDROID VOICE - Sco onConnected" );
329- startListening ();
330- }
331-
332- @ Override
333- public void onDisconnected () {
334- logger .w (TAG , "ANDROID VOICE - Sco onDisconnected" );
335- if (bluetoothSco .isBluetoothScoOn ()) {
336- logger .w (TAG , "ANDROID VOICE - shutdown from Sco" );
337- shutdown ();
338- }
339- }
340- };
341- // Start Bluetooth Sco
342- bluetoothSco .startSco (listener );
343- logger .v (TAG , "ANDROID VOICE - waiting for bluetooth sco connection" );
344- }
345- else {
346- logger .v (TAG , "ANDROID VOICE - bluetooth sco is: " + (bluetoothSco .isBluetoothScoOn () ? "on" : "off" ));
347- startListening ();
348- }
349- }
350-
351- private void startListening () {
352- executorService .submit (() -> {
353- lock .lock ();
354- try {
355- mainHandler .post (() -> {
356- if (speechRecognizer == null ) {
357- speechRecognizer = recognizerCreator .create ();
358- logger .v (TAG , "ANDROID VOICE - created" );
359- }
360- recognitionListener .startTimeout ();
361- recognitionListener .setRmsDebug (rmsDebug );
362- if (noSoundThreshold > 0 ) recognitionListener .setNoSoundThreshold (noSoundThreshold );
363- if (lowSoundThreshold > 0 ) recognitionListener .setLowSoundThreshold (lowSoundThreshold );
364- logger .i (TAG , "ANDROID VOICE - start listening" );
365- speechRecognizer .setRecognitionListener (recognitionListener );
366- //adjustVolumeForBeep();
367- speechRecognizer .startListening (speechRecognizerIntent );
368- executorService .submit (lock ::unlock );
369- });
370- } catch (Exception e ) {
371- logger .logException (e );
372- lock .unlock ();
373- }
374- });
375- }
376-
377- private void handleListeners (RecognizerListener ... listeners ) {
378- recognitionListener .reset ();
379- if (listeners != null && listeners .length > 0 ) {
380- for (RecognizerListener item : listeners ) {
381- if (item instanceof OnRecognizerReady ) {
382- recognitionListener .setOnReady ((OnRecognizerReady ) item );
383- }
384- else if (item instanceof OnRecognizerResults ) {
385- recognitionListener .setOnResults ((OnRecognizerResults ) item );
386- }
387- else if (item instanceof OnRecognizerMostConfidentResult ) {
388- recognitionListener .setOnMostConfidentResult ((OnRecognizerMostConfidentResult ) item );
389- }
390- else if (item instanceof OnRecognizerPartialResults ) {
391- recognitionListener .setOnPartialResults ((OnRecognizerPartialResults ) item );
392- }
393- else if (item instanceof OnRecognizerError ) {
394- recognitionListener .setOnError ((OnRecognizerError ) item );
395- }
396- }
397- }
398- }
399-
400- public void setTryAgain (boolean tryAgain ) {
401- this .recognitionListener .setTryAgain (tryAgain );
402- }
403-
404- public void setPartialResults (boolean partial ) {
405- this .speechRecognizerIntent .putExtra (RecognizerIntent .EXTRA_PARTIAL_RESULTS , partial );
406- }
407-
408- public void setRmsDebug (boolean rmsDebug ) {
409- this .rmsDebug = rmsDebug ;
410- }
411-
412- public void setNoSoundThreshold (float maxValue ) {
413- this .noSoundThreshold = maxValue ;
414- }
415-
416- public void setLowSoundThreshold (float maxValue ) {
417- this .lowSoundThreshold = maxValue ;
418- }
419-
420385 public static String getErrorType (int error ) {
421386 switch (error ) {
422387 case android .speech .SpeechRecognizer .ERROR_AUDIO :
0 commit comments