Example usage for com.amazonaws.services.polly.model SynthesizeSpeechResult getContentType

List of usage examples for com.amazonaws.services.polly.model SynthesizeSpeechResult getContentType

Introduction

In this page you can find the example usage for com.amazonaws.services.polly.model SynthesizeSpeechResult getContentType.

Prototype


public String getContentType() 

Source Link

Document

Specifies the type audio stream.

Usage

From source file:com.github.gregwhitaker.awspolly.example.PollyReadHandler.java

License:Apache License

@Override
public void handle(Context ctx) throws Exception {
    String voiceId = ctx.getRequest().getQueryParams().get("voiceId");
    String text = ctx.getRequest().getQueryParams().get("text");
    String outputFormat = ctx.getRequest().getQueryParams().get("outputFormat");

    SynthesizeSpeechRequest ssRequest = new SynthesizeSpeechRequest();
    ssRequest.setVoiceId(voiceId);/*from  w w w  .j  ava  2  s.c o m*/
    ssRequest.setOutputFormat(outputFormat);
    ssRequest.setText(text);

    SynthesizeSpeechResult result = polly.synthesizeSpeech(ssRequest);

    ctx.getResponse().contentType(result.getContentType());
    ctx.getResponse().sendStream(s -> s.onSubscribe(new Subscription() {
        @Override
        public void request(long n) {
            try {
                byte[] data = new byte[1024];
                int bytesRead = result.getAudioStream().read(data);

                while (bytesRead != -1) {
                    s.onNext(Unpooled.wrappedBuffer(data));
                    bytesRead = result.getAudioStream().read(data);
                }
            } catch (IOException e) {
                ctx.getResponse().status(500);
                ctx.getResponse().send();
            } finally {
                s.onComplete();
            }
        }

        @Override
        public void cancel() {

        }
    }));
}