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();

}

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 2015年11月25日摩天轮的蓝色天空!地点:郑州→人民公园
    瑞鸽阅读 465评论 0 0
  • 老子警告你,在老子生气的时候,不要跟老子嬉皮笑脸的,因为,你一笑,老子也要跟着你一起笑,老子一笑,那老子就非...
    1朵粑粑花阅读 338评论 0 1

友情链接更多精彩内容