微软Azure文字转语音api调用及前后端联动通过文件流下载保姆级教程

遇到问题:后端返回文件流,前端下载下来文件损坏无法打开播放,
解决办法:设置音频格式为期望的格式,例如我用的是
SpeechSynthesisOutputFormat.Audio48Khz96KBitRateMonoMp3
这是mp3文件的音频格式

  1. 获得Azure文字转语音的资源,这个自行获取,微软也提供了免费额度。
  2. 拿到密钥和地区这两个信息。
  3. 后端pom.xml引入微软语音SDK依赖
<dependencies>
	<!-- 其他依赖-->
	
	<!-- 微软语音服务SDK-->
	<dependency>
	    <groupId>com.microsoft.cognitiveservices.speech</groupId>
	    <artifactId>client-sdk</artifactId>
	    <version>1.37.0</version>
	</dependency>
</dependencies>
  1. 后端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);
    }
}
  1. 后端接口定义ISpeechService.java
package com.ruoyi.word.service;
import javax.servlet.http.HttpServletResponse;

public interface ISpeechService {
    /**
     * 将文本转换为语音并返回音频字节数组。
     *
     * @param content 需要转换为语音的文本
     */
    void convertTextToSpeech(HttpServletResponse response, String content);
}
  1. 后端接口实现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();
        }
    }
}
  1. 前端发送请求,下载文件,我是微信小程序端,使用wx.request请求后端,后续再更新
  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值