Text-to-speech Java SDK
The Voice RSS Text-to-Speech Java SDK wraps Voice RSS Text-to-Speech API.
The Voice RSS Text-to-Speech Java SDKs will help to make integration with our Text-to-Speech API faster and easer.
If you have any questions or suggestions please feel free to contact us via e-mail.
Documentation
To integrate an application with the Voice RSS Text-to-Speech Java SDK it needs to add reference to the package voicerss_tts.jar. The Voice RSS Text-to-Speech Java SDK implements converting text-to-speech synchronously and asynchronously. The Voice RSS Text-to-Speech Java SDK provides possibility to get speech in binary or Base64 string formats.
SDK specifications
Convert text-to-speech synchronously
The following example demonstrates synchronous converting text-to-speech as a binary array:
import java.io.FileOutputStream; import com.voicerss.tts.AudioCodec; import com.voicerss.tts.AudioFormat; import com.voicerss.tts.Languages; import com.voicerss.tts.SpeechDataEvent; import com.voicerss.tts.SpeechDataEventListener; import com.voicerss.tts.SpeechErrorEvent; import com.voicerss.tts.SpeechErrorEventListener; import com.voicerss.tts.VoiceParameters; import com.voicerss.tts.VoiceProvider; public class Example { public static void main (String args[]) throws Exception { VoiceProvider tts = new VoiceProvider("<API key>"); VoiceParameters params = new VoiceParameters("Hello, world!", Languages.English_UnitedStates); params.setCodec(AudioCodec.WAV); params.setFormat(AudioFormat.Format_44KHZ.AF_44khz_16bit_stereo); params.setBase64(false); params.setSSML(false); params.setRate(0); byte[] voice = tts.speech(params); FileOutputStream fos = new FileOutputStream("voice.mp3"); fos.write(voice, 0, voice.length); fos.flush(); fos.close(); } }
Convert text-to-speech asynchronously
The following example demonstrates asynchronous converting text-to-speech as a binary array based on event pattern:
import java.io.FileOutputStream; import com.voicerss.tts.AudioCodec; import com.voicerss.tts.AudioFormat; import com.voicerss.tts.Languages; import com.voicerss.tts.SpeechDataEvent; import com.voicerss.tts.SpeechDataEventListener; import com.voicerss.tts.SpeechErrorEvent; import com.voicerss.tts.SpeechErrorEventListener; import com.voicerss.tts.VoiceParameters; import com.voicerss.tts.VoiceProvider; public class Example { public static void main (String args[]) throws Exception { VoiceProvider tts = new VoiceProvider("<API key>"); VoiceParameters params = new VoiceParameters("Hello, world!", Languages.English_UnitedStates); params.setCodec(AudioCodec.WAV); params.setFormat(AudioFormat.Format_44KHZ.AF_44khz_16bit_stereo); params.setBase64(false); params.setSSML(false); params.setRate(0); tts.addSpeechErrorEventListener(new SpeechErrorEventListener() { @Override public void handleSpeechErrorEvent(SpeechErrorEvent e) { System.out.print(e.getException().getMessage()); } }); tts.addSpeechDataEventListener(new SpeechDataEventListener() { @Override public void handleSpeechDataEvent(SpeechDataEvent e) { try { byte[] voice = (byte[])e.getData(); FileOutputStream fos = new FileOutputStream("voice.mp3"); fos.write(voice, 0, voice.length); fos.flush(); fos.close(); } catch (Exception ex) { ex.printStackTrace(); } } }); tts.speechAsync(params); } }
Convert text-to-speech as a Base64 string
The following example demonstrates synchronous converting text-to-speech as a Base64 string and prints HTML audio tag with audio content to play in an internet browser:
import java.io.FileOutputStream; import com.voicerss.tts.AudioCodec; import com.voicerss.tts.AudioFormat; import com.voicerss.tts.Languages; import com.voicerss.tts.SpeechDataEvent; import com.voicerss.tts.SpeechDataEventListener; import com.voicerss.tts.SpeechErrorEvent; import com.voicerss.tts.SpeechErrorEventListener; import com.voicerss.tts.VoiceParameters; import com.voicerss.tts.VoiceProvider; public class Example { public static void main (String args[]) throws Exception { VoiceProvider tts = new VoiceProvider("<API key>"); VoiceParameters params = new VoiceParameters("Hello, world!", Languages.English_UnitedStates); params.setCodec(AudioCodec.WAV); params.setFormat(AudioFormat.Format_44KHZ.AF_44khz_16bit_stereo); params.setBase64(false); params.setSSML(false); params.setRate(0); String voice = tts.speech(params); System.out.print(String.format("<audio src='%s' autoplay='autoplay'></audio>", voice)); } }