Skip to content

Instantly share code, notes, and snippets.

@IT-Berater
Created September 1, 2018 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IT-Berater/ed75f8ba84f85378713d61f9ecc24f8e to your computer and use it in GitHub Desktop.
Save IT-Berater/ed75f8ba84f85378713d61f9ecc24f8e to your computer and use it in GitHub Desktop.
Text to MP3 erstellen
package de.wenzlaff.sound;
import java.io.FileOutputStream;
import com.voicerss.tts.AudioCodec;
import com.voicerss.tts.AudioFormat;
import com.voicerss.tts.Languages;
import com.voicerss.tts.VoiceParameters;
import com.voicerss.tts.VoiceProvider;
/**
* Beispiel für Text to MP3 Datei.
*
* Lib Download von http://www.voicerss.org/sdk/java.aspx
*
* @author Thomas Wenzlaff
*
*/
public class Start {
private static final String TEXT = "Hallo Thomas, das ist aber cool! Ich finde Java gut. Damit kann man sehr einfach eine MP3 Datei erstellen.";
private static final String MP3_DATEI_NAME = "thomas-cool.mp3";
private static final String API_KEY = "TODO API Key eintragen";
public static void main(String[] args) throws Exception {
textToMp3(TEXT, MP3_DATEI_NAME);
}
/**
* Methode erstellt eine MP3 Datei aus dem Text
*
* @param text der Inhalt der MP3 Datei
* @param dateiname der Name der MP3 Datei
* @throws Exception bei Fehler
*/
private static void textToMp3(String text, String dateiname) throws Exception {
VoiceProvider tts = new VoiceProvider(API_KEY);
VoiceParameters params = new VoiceParameters(text, Languages.German);
params.setCodec(AudioCodec.MP3);
params.setFormat(AudioFormat.Format_44KHZ.AF_44khz_16bit_mono);
params.setBase64(false);
params.setSSML(false);
params.setRate(0);
byte[] voice = tts.speech(params);
FileOutputStream fos = new FileOutputStream(dateiname);
fos.write(voice, 0, voice.length);
fos.flush();
fos.close();
}
}
@IT-Berater
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment