use of com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.LexClientException 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();
}
use of com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.LexClientException in project aws-sdk-android by aws-amplify.
the class InteractionClient method processResponseAudioPlayback.
/**
* Invokes the Android {@link MediaPlayer} to playback audio if audio
* playback was requested, and continues to analyze the response. If the
* response does not contain audio stream or if audio playback was not
* requested, continues to analyze the response.
*
* @param handler {@link Handler}, to interact with app components in the
* main thread.
* @param result {@link PostContentResult}, response from the Amazon Lex
* service.
* @param client {@link InteractionClient}, reference to this object.
* @param responseMode {@link ResponseType}, current response type.
*/
private void processResponseAudioPlayback(final Handler handler, final PostContentResult result, final InteractionClient client, final ResponseType responseMode, final ResponseType requestType) {
// Check if response is audio and audio playback is requested.
if (ResponseType.AUDIO_MPEG.equals(responseMode) && interactionConfig.isEnableAudioPlayback()) {
this.lMediaPlayer = new MediaPlayer();
this.lMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
setAudioPlaybackState(BUSY);
File tempAudioFile = File.createTempFile("lex_temp_response", "mp3", context.getFilesDir());
tempAudioFile.deleteOnExit();
// Media player listeners.
lMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(final MediaPlayer mp, final int what, final int extra) {
if (interactionListener != null) {
final Runnable appCallback = new Runnable() {
@Override
public void run() {
audioPlaybackListener.onAudioPlaybackError(new AudioPlaybackException(String.format(Locale.US, "MediaPlayer error: \"what\": %d, \"extra\":%d", what, extra)));
}
};
handler.post(appCallback);
}
return false;
}
});
lMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (audioPlaybackListener != null) {
final Runnable appCallback = new Runnable() {
@Override
public void run() {
audioPlaybackListener.onAudioPlaybackStarted();
}
};
handler.post(appCallback);
}
mp.start();
}
});
lMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
setAudioPlaybackState(NOT_BUSY);
if (audioPlaybackListener != null) {
final Runnable appCallback = new Runnable() {
@Override
public void run() {
audioPlaybackListener.onAudioPlayBackCompleted();
}
};
handler.post(appCallback);
}
try {
if (lMediaPlayer.isPlaying() || lMediaPlayer.isLooping()) {
lMediaPlayer.stop();
}
lMediaPlayer.release();
} catch (final Exception e) {
Log.e(TAG, "InteractionClient: Error while releasing MediaPlayer", e);
} finally {
lMediaPlayer = null;
}
}
});
final InputStream audioStream = result.getAudioStream();
tempAudioFile = File.createTempFile("lex_temp_response", "dat", context.getFilesDir());
tempAudioFile.deleteOnExit();
final FileOutputStream audioOut = new FileOutputStream(tempAudioFile);
final byte[] buffer = new byte[16384];
int length;
while ((length = audioStream.read(buffer)) != -1) {
audioOut.write(buffer, 0, length);
}
audioOut.close();
final FileInputStream audioIn = new FileInputStream(tempAudioFile);
lMediaPlayer.setDataSource(audioIn.getFD());
lMediaPlayer.prepare();
processResponse(handler, result, client, responseMode, requestType);
} catch (final Exception e) {
// Playback failed.
if (audioPlaybackListener != null) {
final Runnable appCallback = new Runnable() {
@Override
public void run() {
audioPlaybackListener.onAudioPlaybackError(new LexClientException("Audio playback failed", e));
}
};
handler.post(appCallback);
}
try {
if (lMediaPlayer.isPlaying() || lMediaPlayer.isLooping()) {
lMediaPlayer.stop();
}
lMediaPlayer.release();
lMediaPlayer = null;
} catch (final Exception exp) {
Log.e(TAG, "InteractionClient: Error while releasing MediaPlayer", exp);
}
processResponse(handler, result, client, responseMode, requestType);
} finally {
setAudioPlaybackState(NOT_BUSY);
}
} else {
processResponse(handler, result, client, responseMode, requestType);
}
}
Aggregations