thesonpb

textToSpeech

Oct 22nd, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. public void textToSpeech(ActionEvent actionEvent) throws Exception {
  2.         VoiceProvider tts = new VoiceProvider("2130d5966a01404e94526dd3ccdb4062");
  3.  
  4.         VoiceParameters params = new VoiceParameters(textField.getText(), Languages.English_UnitedStates);
  5.         params.setCodec(AudioCodec.WAV);
  6.         params.setFormat(AudioFormat.Format_44KHZ.AF_44khz_16bit_stereo);
  7.         params.setBase64(false);
  8.         params.setSSML(false);
  9.         params.setRate(0);
  10.  
  11.         byte[] voice = tts.speech(params);
  12.  
  13.         FileOutputStream fos = new FileOutputStream("src\\voice.mp3");
  14.         fos.write(voice, 0, voice.length);
  15.         fos.flush();
  16.         fos.close();
  17.  
  18.  
  19.     //play mp3 file
  20.         try {
  21.             AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(this.getClass().getResource("voice.mp3"));
  22.             Clip clip = AudioSystem.getClip();
  23.             clip.open(audioInputStream);
  24.             clip.start();
  25.         } catch (Exception ex) {
  26.             ex.printStackTrace();
  27.         }
  28.  
  29.     }
Add Comment
Please, Sign In to add comment