使用文件接收音频文件即可跳过播报过程:
package com.tts;
import com.microsoft.cognitiveservices.speech.*;
import com.microsoft.cognitiveservices.speech.audio.AudioConfig;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
/**
* 文本转语音
* @author dftdla
*/
public class SpeechSynthesis {
private static final String speechKey;
private static final String speechRegion;
//情绪度
private static final String styleDegree = "1.4";
//情绪
private static final String style = "excited";
//停顿点持续时间
private static final String commasilenceExact = "600ms";
//设置超时时间为5s
private static final long timeout = 5;
private static final TimeUnit unit = TimeUnit.SECONDS;
public static void main(String[] args) throws InterruptedException, ExecutionException, IOException {
//替换成自己的key和regin
SpeechConfig speechConfig = SpeechConfig.fromSubscription(speechKey, speechRegion);
//azure中菲律宾的女音 : fil-PH-BlessicaNeural
speechConfig.setSpeechSynthesisVoiceName("en-IE-EmilyNeural");
SpeechSynthesizer speechSynthesizer;
String text;
Scanner scanner = new Scanner(System.in);
text = scanner.next();
speechConfig.setSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Riff24Khz16BitMonoPcm);
File file = new File("E:\\home\\admin\\source\\upload\\audio\\00a4da29-eec2-4464-8315-d9d7789f7240.wav");
file.getParentFile().mkdirs();
AudioConfig audioConfig = AudioConfig.fromWavFileOutput(file.getAbsolutePath());
try {
speechSynthesizer = new SpeechSynthesizer(speechConfig, audioConfig);
SpeechSynthesisResult result = speechSynthesizer.SpeakText(text);
speechSynthesizer.StartSpeakingText(Arrays.toString(result.getAudioData()));
// speechSynthesizer.waitUntilDone();
speechSynthesizer.StopSpeakingAsync();
} catch(Exception ex) {
System.out.println(ex.getMessage()+"??");
} finally {
System.out.println("done");
}
System.exit(0);
}
}