再也不用付費(fèi)!Spring Boot + EdgeTTS 實(shí)現(xiàn)媲美真人的語(yǔ)音合成
在語(yǔ)音驅(qū)動(dòng)應(yīng)用日益普及的今天——無(wú)論是智能語(yǔ)音助手、自動(dòng)播報(bào)系統(tǒng),還是內(nèi)容朗讀服務(wù)——文本轉(zhuǎn)語(yǔ)音(TTS, Text-to-Speech) 都成為了關(guān)鍵技術(shù)環(huán)節(jié)。 但對(duì)于 Java 開(kāi)發(fā)者而言,生態(tài)中缺乏如 Python 那樣完善的 Edge TTS 客戶(hù)端支持,這常常讓人頭疼。
好消息是,現(xiàn)在我們可以借助 UnifiedTTS 提供的開(kāi)放 API,免費(fèi)調(diào)用 EdgeTTS 服務(wù)。 更棒的是,它不僅兼容 EdgeTTS,還可平滑切換至 Azure TTS、MiniMax TTS、ElevenLabs TTS 等多種語(yǔ)音模型,讓開(kāi)發(fā)者能夠自由在多音色與多模型間切換,而無(wú)需改動(dòng)核心業(yè)務(wù)邏輯。
本文將帶你從零搭建一個(gè)基于 Spring Boot 的語(yǔ)音合成服務(wù),實(shí)現(xiàn)輸入文本 → 輸出真人語(yǔ)音 MP3 文件的完整流程。
創(chuàng)建 Spring Boot 項(xiàng)目
我們先通過(guò) start.spring.io 快速構(gòu)建項(xiàng)目骨架,添加 Web 模塊以提供 REST 接口服務(wù)。
Maven 依賴(lài)配置
<dependencies>
<!-- Spring Boot Web 模塊 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>目錄結(jié)構(gòu)示例(Linux 路徑格式):
/src
└── main
├── java
│ └── com
│ └── icoderoad
│ └── tts
│ ├── UnifiedTtsProperties.java
│ ├── UnifiedTtsService.java
│ └── controller
│ └── TtsController.java
└── resources
└── application.properties獲取 UnifiedTTS API Key
- 打開(kāi) UnifiedTTS 官網(wǎng),使用 GitHub 一鍵登錄;
- 進(jìn)入左側(cè)菜單的 “API 密鑰” 頁(yè)面;
- 點(diǎn)擊“創(chuàng)建 API Key”,復(fù)制保存。
這個(gè) Key 將作為后續(xù)調(diào)用接口的憑證。
接入 UnifiedTTS 接口
我們將按照官方文檔(https://unifiedtts.com/zh/api-docs/tts-sync)實(shí)現(xiàn)一個(gè)完整的 TTS 方案,包括:
- 配置文件;
- 請(qǐng)求/響應(yīng)數(shù)據(jù)模型;
- 服務(wù)層封裝;
- 測(cè)試與文件輸出。
配置文件
文件路徑:/src/main/resources/application.properties
unified-tts.host=https://unifiedtts.com
unified-tts.api-key=your-api-key-here配置類(lèi)
文件路徑:/src/main/java/com/icoderoad/tts/UnifiedTtsProperties.java
package com.icoderoad.tts;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* UnifiedTTS 配置屬性綁定類(lèi)
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "unified-tts")
public class UnifiedTtsProperties {
/** 接口主機(jī)地址 */
private String host;
/** API Key(從 UnifiedTTS 控制臺(tái)獲?。?*/
private String apiKey;
}請(qǐng)求與響應(yīng)模型
文件路徑:/src/main/java/com/icoderoad/tts/model/UnifiedTtsRequest.java
package com.icoderoad.tts.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* UnifiedTTS 請(qǐng)求參數(shù)模型
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UnifiedTtsRequest {
private String model;
private String voice;
private String text;
private Double speed;
private Double pitch;
private Double volume;
private String format;
}服務(wù)實(shí)現(xiàn)類(lèi)
文件路徑:/src/main/java/com/icoderoad/tts/service/UnifiedTtsService.java
package com.icoderoad.tts.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* UnifiedTTS 響應(yīng)結(jié)果封裝
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UnifiedTtsResponse {
private boolean success;
private String message;
private long timestamp;
private UnifiedTtsResponseData data;
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class UnifiedTtsResponseData {
@JsonProperty("request_id")
private String requestId;
@JsonProperty("audio_url")
private String audioUrl;
@JsonProperty("file_size")
private long fileSize;
}
}單元測(cè)試驗(yàn)證
文件路徑:/src/test/java/com/icoderoad/tts/UnifiedTtsServiceTest.java
package com.icoderoad.tts.service;
import com.icoderoad.tts.UnifiedTtsProperties;
import com.icoderoad.tts.model.UnifiedTtsRequest;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* UnifiedTTS 語(yǔ)音合成服務(wù)
*/
@Service
public class UnifiedTtsService {
private final RestClient restClient;
private final UnifiedTtsProperties properties;
public UnifiedTtsService(RestClient restClient, UnifiedTtsProperties properties) {
this.restClient = restClient;
this.properties = properties;
}
/**
* 調(diào)用 UnifiedTTS 接口生成音頻字節(jié)流
*/
public byte[] synthesize(UnifiedTtsRequest request) {
ResponseEntity<byte[]> response = restClient.post()
.uri(properties.getHost() + "/api/v1/common/tts-sync")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_OCTET_STREAM, MediaType.valueOf("audio/mpeg"))
.header("X-API-Key", properties.getApiKey())
.body(request)
.retrieve()
.toEntity(byte[].class);
if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {
return response.getBody();
}
throw new IllegalStateException("UnifiedTTS synthesize failed: " + response.getStatusCode());
}
/**
* 將生成的音頻寫(xiě)入文件
*/
public Path synthesizeToFile(UnifiedTtsRequest request, Path outputPath) {
byte[] data = synthesize(request);
try {
if (outputPath.getParent() != null) {
Files.createDirectories(outputPath.getParent());
}
Files.write(outputPath, data);
return outputPath;
} catch (IOException e) {
throw new RuntimeException("Failed to write TTS output: " + outputPath, e);
}
}
}運(yùn)行與效果驗(yàn)證
執(zhí)行測(cè)試后,項(xiàng)目目錄下會(huì)自動(dòng)生成 /test-result/xxxx.mp3 文件。 播放后你將聽(tīng)到自然流暢的語(yǔ)音效果,幾乎可與真人語(yǔ)音媲美。
常用參數(shù)與音色配置
- model:選擇語(yǔ)音模型,如
edge-tts - voice:音色(如
en-US-JennyNeural) - format:輸出格式(支持
mp3、wav等) - speed/pitch/volume:語(yǔ)速、音調(diào)、音量可調(diào)節(jié)
詳細(xì)音色清單與參數(shù)請(qǐng)參考官方文檔: ?? https://unifiedtts.com/zh/api-docs/tts-sync
結(jié)語(yǔ)
本文展示了如何在 Spring Boot 項(xiàng)目中快速集成 UnifiedTTS,并調(diào)用免費(fèi)的 EdgeTTS 服務(wù),實(shí)現(xiàn)高品質(zhì)的文本轉(zhuǎn)語(yǔ)音功能。 通過(guò) UnifiedTTS 的統(tǒng)一接口,你無(wú)需維護(hù)多個(gè)廠(chǎng)商 SDK,就能輕松切換語(yǔ)音模型與音色,實(shí)現(xiàn)更靈活的語(yǔ)音播報(bào)功能。
在生產(chǎn)環(huán)境中,你還可以進(jìn)一步完善以下能力:
- 增加緩存與音頻重用;
- 增強(qiáng)錯(cuò)誤重試與異常監(jiān)控;
- 實(shí)現(xiàn)并發(fā)任務(wù)隊(duì)列與異步處理。
這樣,一個(gè)高性能、可擴(kuò)展、成本為零的語(yǔ)音合成系統(tǒng)就大功告成了。































