use of com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.MaxSpeechTimeOutException in project aws-sdk-android by aws-amplify.
the class InteractionClient method startListening.
/**
* Starts listening to the user over the mic.
*
* @param handler {@link Handler}, to interact with app components in the
* main thread.
* @param microphoneListener {@link MicrophoneListener}, callback to
* communicate recording over microphone to the application.
* @param lexAudioRecorder {@link LexAudioRecorder}, listens to audio from
* mic.
*/
private void startListening(final Handler handler, final MicrophoneListener microphoneListener, final LexAudioRecorder lexAudioRecorder, final PostContentRequest request, final InteractionClient client, final ResponseType mode) {
final AudioRecordingTask recordingTask = new AudioRecordingTask(lexAudioRecorder, new AudioRecordingTaskListener() {
@Override
public void onReadyForSpeech() {
// Client ready to listen to user speech.
if (microphoneListener != null) {
final Runnable appCallBack = new Runnable() {
@Override
public void run() {
microphoneListener.readyForRecording();
}
};
handler.post(appCallBack);
}
}
@Override
public void onBeginningOfSpeech() {
// App detected speech.
if (microphoneListener != null) {
final Runnable appCallBack = new Runnable() {
@Override
public void run() {
microphoneListener.startedRecording();
}
};
handler.post(appCallBack);
}
// Now since the speech frames have been detected, send
// the request to the Amazon Lex bot.
sendAudioRequest(handler, request, client, mode);
}
@Override
public void onBufferReceived(byte[] buffer) {
// No operation required. This callback is invoked by AudioRecorder. The bytes received
// in this callback are PCM encoded. LexAudioRecorder extends AudioRecorder to
// allow other audio encoders, and pipe the encoded bytes through a PipedInputStream.
// The PipedInputStream used in the request to the
// Amazon Lex service.
}
@Override
public void onRmsChanged(final float rmsdB) {
// Sound level has changed.
if (microphoneListener != null) {
final Runnable appCallBack = new Runnable() {
@Override
public void run() {
microphoneListener.onSoundLevelChanged(rmsdB);
}
};
handler.post(appCallBack);
}
}
@Override
public void onSilenceDetected() {
// Silence detected after speech.
if (microphoneListener != null) {
final Runnable appCallBack = new Runnable() {
@Override
public void run() {
microphoneListener.onRecordingEnd();
}
};
handler.post(appCallBack);
}
}
@Override
public void onNoSpeechTimeout() {
// Stop recording on no timeout.
lexAudioRecorder.cancel();
if (microphoneListener != null) {
final Runnable appCallBack = new Runnable() {
@Override
public void run() {
microphoneListener.onMicrophoneError(new NoSpeechTimeOutException("User did not respond within the speech time out limit."));
}
};
handler.post(appCallBack);
}
setBusyState(NOT_BUSY);
}
@Override
public void onMaxSpeechTimeout() {
lexAudioRecorder.cancel();
if (microphoneListener != null) {
final Runnable appCallBack = new Runnable() {
@Override
public void run() {
microphoneListener.onMicrophoneError(new MaxSpeechTimeOutException("User did not complete response within the max speech time out limit."));
}
};
handler.post(appCallBack);
}
}
@Override
public void onError(final AmazonClientException e) {
if (microphoneListener != null) {
final Runnable appCallBack = new Runnable() {
@Override
public void run() {
microphoneListener.onMicrophoneError(new LexClientException(e.getMessage(), e));
}
};
handler.post(appCallBack);
}
}
});
recordingTask.execute();
}
Aggregations