本文介绍的是Amazon polly,可以实现文本转mp3等音频文件,调研了很长时间,这款产品无论是音质、音准和连贯性都比较好,废话不多说,现在开始!
Amazon Polly介绍页面:https://aws.amazon.com/cn/polly/
Amazon Polly开发文档:https://aws.amazon.com/cn/polly/resources/
Amazon Polly 定价: https://aws.amazon.com/cn/polly/pricing/
第一步、需要注册aws(注册地址:https://portal.aws.amazon.com/billing/signup#/start)
经过这两步,aws帐号注册成功!
第二步、需要获得安全凭证(accessKey、secretKey)
第三步、程序代码(注:需要替换程序中的accessKey和secretKey)
1、导入jar包
<dependencies>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-polly -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-polly</artifactId>
<version>1.11.77</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.soundlibs/jlayer -->
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>jlayer</artifactId>
<version>1.0.1-1</version>
</dependency>
</dependencies>
2、程序代码
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.polly.AmazonPolly;
import com.amazonaws.services.polly.AmazonPollyClient;
import com.amazonaws.services.polly.AmazonPollyClientBuilder;
import com.amazonaws.services.polly.model.OutputFormat;
import com.amazonaws.services.polly.model.SynthesizeSpeechRequest;
import com.amazonaws.services.polly.model.SynthesizeSpeechResult;
import com.amazonaws.services.polly.model.VoiceId;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
public class SynthesizeSpeechSample {
static AmazonPolly client;// = AmazonPollyClientBuilder.defaultClient();
public static void synthesizeSpeech() {
String outputFileName = "d:\\tmp\\speech.mp3";
String text = "WASHINGTON - US President Donald Trump and the top leader of the Democratic " +
"People's Republic of Korea (DPRK), Kim Jong Un, are scheduled to meet at 9:00 am Singapore time on June 12, " +
"the White House said on Monday. At a press briefing, White House spokesperson Sarah Sanders also said "+
"that the U.S. advance team now in Singapore \"is finalizing logistical preparations and will remain in place " +
"until the summit begins.\" Noting that the US side is \"actively\" preparing for the meeting, " +
"she said that the US delegation in the demilitarized zone (DMZ) along the inter-Korean border continues" +
" diplomatic negotiations with the DPRK delegation.";
SynthesizeSpeechRequest synthesizeSpeechRequest = new SynthesizeSpeechRequest()
.withOutputFormat(OutputFormat.Mp3)
.withVoiceId(VoiceId.Joanna)
.withText(text);
try (FileOutputStream outputStream = new FileOutputStream(new File(outputFileName))) {
SynthesizeSpeechResult synthesizeSpeechResult = client.synthesizeSpeech(synthesizeSpeechRequest);
byte[] buffer = new byte[2 * 1024];
int readBytes;
try (InputStream in = synthesizeSpeechResult.getAudioStream()){
while ((readBytes = in.read(buffer)) > 0) {
outputStream.write(buffer, 0, readBytes);
}
}
System.out.println("音频输出成功!");
} catch (Exception e) {
System.err.println("Exception caught: " + e);
}
}
public static void main(String[] args) {
client = new AmazonPollyClient(new BasicAWSCredentials("AKIAJV25POJZNMD6XXXX","Gr5eJN3jA7hhbds0IUdFSWuSC0DkbrPh5VSNXXXX"));
client.setRegion(Region.getRegion(Regions.US_EAST_1));
synthesizeSpeech();
}
}
至此,此文结束!