微信小程序集成 “百度语音合成” 功能

百度AI开发平台文档:https://ai.baidu.com/ai-doc/SPEECH/jk38y8gno

在这里插入图片描述

1、成为百度AI开放平台的开发者

建立一个百度账号:https://passport.baidu.com/v2/?reg

我们有账号之后登录,并且点击此处创建一个应用,如下图
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
然后就能看到创建完的应用和 API KEY 以及 Secret KEY了
在这里插入图片描述

2、领取免费额度

创建完应用后,可以到概览页领取语音合成的免费额度。免费额度说明详见语音合成免费额度文档
在这里插入图片描述

3、在微信小程序中使用

需要更改 API KEY 以及 Secret KEY,这两个在上面的步骤有

js:

Page({

	data: {
	  token: '',//百度的token
	},
	
	onLoad() {
	  const innerAudioContext = wx.createInnerAudioContext()
	  const appkey = '*******'; //百度应用的 一个月过期
	  const selKey = '*******';//百度应用的 一个月过期 
	  wx.request({
	  	method:'GET',
		url:`https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=${appkey}&client_secret=${selKey}`,
		header: {
			'content-type': 'application/json' // 默认值
		  },
		 success (res) {
			console.log(res.data)
			if (wx.setInnerAudioOption) {
				  wx.setInnerAudioOption({
					obeyMuteSwitch: false,
					autoplay: true
				  })
				}else {
				  innerAudioContext.obeyMuteSwitch = false;
				  innerAudioContext.autoplay = true;
				}
			//监听各个阶段
			innerAudioContext.onCanplay(() => {
			  console.log('可以播放');
			});
			innerAudioContext.onError((res) => {
			  console.log(res.errMsg);
			  console.log(res.errCode);
			});
			innerAudioContext.autoplay = true
			innerAudioContext.src = 'https://tsn.baidu.com/text2audio?lan=zh&ctp=1&cuid=abcdxxx&tok='+res.data.access_token+'&tex=' + encodeURIComponent('哈哈哈哈哈哈哈哈哈') + '&vol=5&per=0&spd=5&pit=5&aue=3';
			innerAudioContext.onPlay(() => {
				console.log('开始播放')
			})
		  }
	  })
	},
	methods: {
	}
})

参数:
在这里插入图片描述

4、java中使用

加入依赖:

       <!--json-->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.47</version>
		</dependency>

		<!--百度语音识别-->
		<dependency>
			<groupId>com.baidu.aip</groupId>
			<artifactId>java-sdk</artifactId>
			<version>4.1.1</version>
		</dependency>

		<!--mp3转pcm-->
		<dependency>
			<groupId>com.googlecode.soundlibs</groupId>
			<artifactId>mp3spi</artifactId>
			<version>1.9.5.4</version>
		</dependency>

SpeechUtil.java

也是需要替换 APP_ID、API_KEY 和 SECRET_KEY


import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.speech.TtsResponse;
import com.baidu.aip.util.Util;
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import java.io.File;
import java.io.IOException;

/**
 * 百度语音工具类
 */
@Slf4j
public class SpeechUtil {


    public static final String APP_ID = "***";

    public static final String API_KEY = "***";

    public static final String SECRET_KEY = "***";

    private static AipSpeech client;

    public static void main(String[] args) throws IOException {
        SpeechSynthesizer("简单测试百度语音合成", "d:/SpeechSynthesizer.mp3");
        convertMP32Pcm("d:/SpeechSynthesizer.mp3","d:/SpeechSynthesizer.pcm");
        SpeechRecognition("d:/SpeechSynthesizer.pcm","pcm");
    }


    /**
     * 单例 懒加载模式 返回实例
     * @return
     */
    public static AipSpeech getInstance(){
        if (client==null){
            synchronized (AipSpeech.class){
                if (client==null) {
                    client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
                }
            }
        }
        return client;
    }

    /**
     * 语音合成
     * @param word 文字内容
     * @param outputPath 合成语音生成路径
     * @return
     */
    public static boolean SpeechSynthesizer(String word, String outputPath) {
        /*
        最长的长度
         */
        int maxLength = 1024;
        if (word.getBytes().length >= maxLength) {
            return false;
        }
        // 初始化一个AipSpeech
        client = getInstance();

        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
//        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 调用接口
        TtsResponse res = client.synthesis(word, "zh", 1, null);
        byte[] data = res.getData();
        org.json.JSONObject res1 = res.getResult();
        if (data != null) {
            try {
                Util.writeBytesToFileSystem(data, outputPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return true;
        }
        if (res1 != null) {
            log.info(" result : " + res1.toString());
        }
        return false;

    }

    /**
     * 语音识别
     * @param videoPath
     * @param videoType
     * @return
     */
    public static String SpeechRecognition(String videoPath, String videoType) {
        // 初始化一个AipSpeech
        client = getInstance();

        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
//        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理


        // 调用接口
        JSONObject res = client.asr(videoPath, videoType, 16000, null);
        log.info(" SpeechRecognition : " + res.toString());
        return res.toString(2);
    }


    /**
     *  mp3转pcm
     * @param mp3filepath MP3文件存放路径
     * @param pcmfilepath pcm文件保存路径
     * @return
     */
    public static boolean convertMP32Pcm(String mp3filepath, String pcmfilepath){
        try {
            //获取文件的音频流,pcm的格式
            AudioInputStream audioInputStream = getPcmAudioInputStream(mp3filepath);
            //将音频转化为  pcm的格式保存下来
            AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, new File(pcmfilepath));
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获得pcm文件的音频流
     * @param mp3filepath
     * @return
     */
    private static AudioInputStream getPcmAudioInputStream(String mp3filepath) {
        File mp3 = new File(mp3filepath);
        AudioInputStream audioInputStream = null;
        AudioFormat targetFormat = null;
        try {
            AudioInputStream in = null;
            MpegAudioFileReader mp = new MpegAudioFileReader();
            in = mp.getAudioInputStream(mp3);
            AudioFormat baseFormat = in.getFormat();
            targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16,
                    baseFormat.getChannels(), baseFormat.getChannels()*2, baseFormat.getSampleRate(), false);
            audioInputStream = AudioSystem.getAudioInputStream(targetFormat, in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return audioInputStream;
    }

}

参考文章

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值