遇到问题:后端返回文件流,前端下载下来文件损坏无法打开播放,
解决办法:设置音频格式为期望的格式,例如我用的是
SpeechSynthesisOutputFormat.Audio48Khz96KBitRateMonoMp3
这是mp3文件的音频格式
- 获得Azure文字转语音的资源,这个自行获取,微软也提供了免费额度。
- 拿到密钥和地区这两个信息。
- 后端pom.xml引入微软语音SDK依赖
<dependencies>
<!-- 其他依赖-->
<!-- 微软语音服务SDK-->
<dependency>
<groupId>com.microsoft.cognitiveservices.speech</groupId>
<artifactId>client-sdk</artifactId>
<version>1.37.0</version>
</dependency>
</dependencies>
- 后端POST接口
SpeechController.java
package com.ruoyi.word.controller;
import com.ruoyi.word.service.ISpeechService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
@RequestMapping("/speech")
public class SpeechController {
@Autowired
private ISpeechService speechService;
/**
* 将文本转换为语音并返回音频文件
*/
@PostMapping("/synthesize")
public void synthesize(HttpServletResponse response, String content) throws IOException {
speechService.convertTextToSpeech(response, content);
}
}
- 后端接口定义
ISpeechService.java
package com.ruoyi.word.service;
import javax.servlet.http.HttpServletResponse;
public interface ISpeechService {
/**
* 将文本转换为语音并返回音频字节数组。
*
* @param content 需要转换为语音的文本
*/
void convertTextToSpeech(HttpServletResponse response, String content);
}
- 后端接口实现
SpeechServiceImpl.java
package com.ruoyi.word.service.impl;
import com.ruoyi.word.service.ISpeechService;
import com.microsoft.cognitiveservices.speech.*;
import com.microsoft.cognitiveservices.speech.audio.*;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;
import java.io.IOException;
@Service
public class SpeechServiceImpl implements ISpeechService {
private static final String SPEECH_KEY = "<自己的密钥>";
private static final String SPEECH_REGION = "<自己的地区,例如eastasia>";
public void convertTextToSpeech(HttpServletResponse response, String content) {
SpeechConfig speechConfig = SpeechConfig.fromSubscription(SPEECH_KEY, SPEECH_REGION);
//设置语言
speechConfig.setSpeechSynthesisLanguage("zh-CN");
//设置生成音色
speechConfig.setSpeechSynthesisVoiceName("zh-CN-XiaoshuangNeural");
//设置音频格式(重要)
speechConfig.setSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Audio48Khz96KBitRateMonoMp3);
SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer(speechConfig, null);
SpeechSynthesisResult result = speechSynthesizer.SpeakText(content);
//把生成结果转成文件流
AudioDataStream stream = AudioDataStream.fromResult(result);
response.setContentType("audio/mpeg");
response.setHeader("Content-Disposition", "attachment;");
try (ServletOutputStream out = response.getOutputStream()) {
byte[] buffer = new byte[1024];
long len;
while ((len = stream.readData(buffer)) != 0) {
//把文件流逐步写入response,让前端能获取
System.out.print(len);
out.write(buffer, 0, (int) len);
}
out.flush();
} catch (IOException e) {
// 处理IO异常
e.printStackTrace();
}
}
}
- 前端发送请求,下载文件,我是微信小程序端,使用wx.request请求后端,后续再更新