in src/main/java/com/amazonaws/lex/twilio/sample/conversation/TwilioCallOperator.java [87:113]
public void playback(AudioResponse audioResponse) {
try (CompressInputStream responseStream = new CompressInputStream(audioResponse, false)) {
byte[] bytes = new byte[MAX_BYTES_TO_READ];
int numOfBytesRead = responseStream.read(bytes);
//while not end of stream, or not playback paused
while (numOfBytesRead != -1 && !interruptSendingDataToTwilio.get()) {
byte[] copy = Arrays.copyOf(bytes, numOfBytesRead);
MediaMessage mediaMessage = new MediaMessage(copy, callIdentifier.getStreamSid());
writeToStream(mediaMessage.getJsonObject(), false);
bytes = new byte[MAX_BYTES_TO_READ];
numOfBytesRead = responseStream.read(bytes);
}
//if it was not interrupted, it means it has reached end of stream.
if (!interruptSendingDataToTwilio.get()) {
// mark the end of stream and when we get back the same mark, we inform bot that
// playback is complete.
LOG.info("audio stream has ended, marking a message ");
currentPlaybackLabel = Optional.of(UUID.randomUUID().toString());
writeToStream(new MarkMessage(callIdentifier.getStreamSid(), currentPlaybackLabel.get()).getJsonObject(), true);
}
} catch (IOException e) {
e.printStackTrace();
}
}