windows 环境实现文字转语音。

目前有很多提供语音合成的SDK,比如科大讯飞,百度,腾讯云等。

其实windows powershell里自带语音合成。

例如:

Add-Type -AssemblyName System.speech;

$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer;

$speak.Rate =1;//朗读速度

$speak.SetOutputToWaveFile('e:不信,你就试试啊.mp3');//在e盘跟目录生成一个MP3文件。

$speak.Speak('不信,你就试试啊');//要转成语音的文字

还有一个speak.Voice属性是设置音色的,需要下载才能 设置。

详见官方api。

java如何调用ps1(powershell)命令?

public class Main {

public static void main(String[] args) throws Exception {

txt2Audio("不信,你就试试啊","e:");

}

private static void txt2Audio(String txt, String path) throws Exception {

String cmd1 = "Add-Type -AssemblyName System.speech;";

String cmd2 = "$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer;";

String cmd3 = "$speak.Rate =1;";

String cmd4 = "$speak.SetOutputToWaveFile('" + path + txt + ".mp3');";

String cmd5 = "$speak.Speak('" + txt + "')";

String cmd = cmd1 + cmd2 + cmd3 + cmd4 + cmd5;

exec(cmd);

}

private static String exec(String command) throws Exception {

String cmd = "cmd /c c:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell -ExecutionPolicy RemoteSigned -noprofile -noninteractive ";

command = cmd + command;

StringBuffer sbInput = new StringBuffer();

StringBuffer sbError = new StringBuffer();

Runtime runtime = Runtime.getRuntime();

Process proc = runtime.exec(command);

proc.getOutputStream().close();

InputStream inputstream = proc.getInputStream();

InputStreamReader inputstreamreader = new InputStreamReader(inputstream);

BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

String line;

while ((line = bufferedreader.readLine()) != null) {

sbInput.append(line + "\n");

}

inputstream = proc.getErrorStream();

inputstreamreader = new InputStreamReader(inputstream);

bufferedreader = new BufferedReader(inputstreamreader);

while ((line = bufferedreader.readLine()) != null) {

sbError.append(line + "\n");

}

if (sbError.length() > 0)

throw new Exception("The command [" + command

+ "] failed to execute!\n\nResult returned:\n"

+ sbError.toString());

return "The command [" + command

+ "] executed successfully!\n\nResult returned:\n"

+ sbInput.toString();

}

}

你可能感兴趣的:(windows 环境实现文字转语音。)