Example usage for com.amazonaws.services.polly.model SynthesizeSpeechRequest setText

List of usage examples for com.amazonaws.services.polly.model SynthesizeSpeechRequest setText

Introduction

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

Prototype


public void setText(String text) 

Source Link

Document

Input text to synthesize.

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 .  ja  va 2 s  .c  om*/
    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() {

        }
    }));
}

From source file:com.waltercedric.tvprogram.plugins.reader.PollyTTSReader.java

License:Open Source License

public void play(String sentenceToPlay) {
    synchronized (object) {
        SynthesizeSpeechRequest tssRequest = newRequest();

        // TextLengthExceededException
        // The value of the "Text" parameter is longer than the accepted limits. The limit for input text is a
        // maximum of 3000 characters total, of which no more than 1500 can be billed characters.
        // SSML tags are not counted as billed characters.
        // HTTP Status Code: 400
        if (sentenceToPlay.length() > 3000) {
            sentenceToPlay = sentenceToPlay.substring(0, 2999);
        }// w w  w  . j a v a  2 s  . c om

        tssRequest.setText(sentenceToPlay);

        Future<SynthesizeSpeechResult> synthesizeSpeechResultFuture = polly.synthesizeSpeechAsync(tssRequest);
        try {
            InputStream audioStream = synthesizeSpeechResultFuture.get().getAudioStream();
            myplayer.play(audioStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}