@Override public void handle(Context ctx) throws Exception { String token = null; List<Voice> voices = new ArrayList<>(); while (true) { DescribeVoicesResult result; if (token == null) { result = polly.describeVoices(new DescribeVoicesRequest()); } else { result = polly.describeVoices(new DescribeVoicesRequest().withNextToken(token)); } voices.addAll(result.getVoices()); if (result.getNextToken() != null) { token = result.getNextToken(); } else { ctx.render(Jackson.toJsonString(voices)); break; } } }
private void processVoicesRequest() { // Create describe voices request. DescribeVoicesRequest describeVoicesRequest = new DescribeVoicesRequest(); // Synchronously ask Polly Polly to describe available TTS voices. DescribeVoicesResult describeVoicesResult = polly.describeVoices(describeVoicesRequest); awsVoices = describeVoicesResult.getVoices(); log.info("found {} voices", awsVoices.size()); for (int i = 0; i < awsVoices.size(); ++i) { Voice voice = awsVoices.get(i); voiceMap.put(voice.getName(), voice); langMap.put(voice.getLanguageCode(), voice); log.info("{} {} - {}", i, voice.getName(), voice.getLanguageCode()); } // set default voice if (voice == null) { voice = awsVoices.get(0).getName(); awsVoice = awsVoices.get(0); lang = awsVoice.getLanguageCode(); log.info("setting default voice to {}", voice); } }
@Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.voice_spinner_row, parent, false); } Voice voice = voices.get(position); TextView nameTextView = (TextView) convertView.findViewById(R.id.voiceName); nameTextView.setText(voice.getName()); TextView languageCodeTextView = (TextView) convertView.findViewById(R.id.voiceLanguageCode); languageCodeTextView.setText(voice.getLanguageName() + " (" + voice.getLanguageCode() + ")"); return convertView; }
@Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); selectedPosition = savedInstanceState.getInt(KEY_SELECTED_VOICE_POSITION); voices = (List<Voice>) savedInstanceState.getSerializable(KEY_VOICES); String sampleText = savedInstanceState.getString(KEY_SAMPLE_TEXT); if (sampleText.isEmpty()) { defaultTextButton.setVisibility(View.GONE); } else { sampleTextEditText.setText(sampleText); defaultTextButton.setVisibility(View.VISIBLE); } }
String getSampleText(Voice voice) { if (voice == null) { return ""; } String resourceName = "sample_" + voice.getLanguageCode().replace("-", "_").toLowerCase() + "_" + voice.getId().toLowerCase(); int sampleTextResourceId = getResources().getIdentifier(resourceName, "string", getPackageName()); if (sampleTextResourceId == 0) return ""; return getString(sampleTextResourceId); }
void setDefaultTextForSelectedVoice() { Voice selectedVoice = (Voice) voicesSpinner.getSelectedItem(); if (selectedVoice == null) { return; } String sampleText = getSampleText(selectedVoice); sampleTextEditText.setHint(sampleText); }
SpinnerVoiceAdapter(Context ctx, List<Voice> voices) { this.inflater = LayoutInflater.from(ctx); this.voices = voices; }
void setupPlayButton() { playButton = (Button) findViewById(R.id.readButton); playButton.setEnabled(false); playButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { playButton.setEnabled(false); Voice selectedVoice = (Voice) voicesSpinner.getSelectedItem(); String textToRead = sampleTextEditText.getText().toString(); // Use voice's sample text if user hasn't provided any text to read. if (textToRead.trim().isEmpty()) { textToRead = getSampleText(selectedVoice); } // Create speech synthesis request. SynthesizeSpeechPresignRequest synthesizeSpeechPresignRequest = new SynthesizeSpeechPresignRequest() // Set text to synthesize. .withText(textToRead) // Set voice selected by the user. .withVoiceId(selectedVoice.getId()) // Set format to MP3. .withOutputFormat(OutputFormat.Mp3); // Get the presigned URL for synthesized speech audio stream. URL presignedSynthesizeSpeechUrl = client.getPresignedSynthesizeSpeechUrl(synthesizeSpeechPresignRequest); Log.i(TAG, "Playing speech from presigned URL: " + presignedSynthesizeSpeechUrl); // Create a media player to play the synthesized audio stream. if (mediaPlayer.isPlaying()) { setupNewMediaPlayer(); } mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try { // Set media player's data source to previously obtained URL. mediaPlayer.setDataSource(presignedSynthesizeSpeechUrl.toString()); } catch (IOException e) { Log.e(TAG, "Unable to set data source for the media player! " + e.getMessage()); } // Start the playback asynchronously (since the data source is a network stream). mediaPlayer.prepareAsync(); } }); }