当前位置: 首页 > news >正文

Android开发中,百度语音集成之一

我们在开发中,用到实时语音的时候,会有讯飞、百度、阿里,今天主要讲解的是百度语音之语音合成:

public class YuYinUtil {

    private static final Logger logger = LogManager.getLogger(YuYinUtil.class);

    public static final String YU_YIN_HE_CHENG_URL = "http://tsn.baidu.com/text2audio";

    /**
     * 语音合成
     * 
     * @param token 百度token
     * @param text  发音文本
     * @param vol   音量 0-15
     * @param per   发音人 0-普通女声、1-普通男声、3-度逍遥、4度丫丫
     * @param spd   语速 0-15
     * @param aue   格式 3为mp3格式(默认); 4为pcm-16k;5为pcm-8k;6为wav(内容同pcm-16k);
     *              注意aue=4或者6是语音识别要求的格式,但是音频内容不是语音识别要求的自然人发音,所以识别效果会受影响。
     * @throws UnsupportedEncodingException
     */
    @SuppressWarnings("deprecation")
    public void generateYuYin(String token, String text, String vol, String per, String spd, String pit, String aue,
            String outputFile) {

        logger.debug("调用语音合成接口");
        logger.debug("text:" + text);
        logger.debug("vol:" + vol);
        logger.debug("per:" + per);
        logger.debug("spd:" + spd);
        logger.debug("pit:" + pit);
        logger.debug("aue:" + aue);

        CloseableHttpClient httpClient = HttpClients.createMinimal();

        // 百度语音要求tex字段进行两次url encode,post实体中会进行一次encode
        String encodeText;
        try {
            encodeText = URLEncoder.encode(text, "utf-8");
        } catch (UnsupportedEncodingException e1) {
            throw new RuntimeException(e1);
        }
//        encodeText = URLEncoder.encode(encodeText, "utf-8");

//        String url = YU_YIN_HE_CHENG_URL + "?lan=zh&ctp=1&cuid=abcdxxx&tok=" + token + "&tex=" + encodeText
//                + "&vol=9&per=0&spd=5&pit=5&aue=3";
//        url = URLEncoder.encode(url, "utf-8");

//        HttpGet get = new HttpGet(url);

        HttpPost post = new HttpPost(YU_YIN_HE_CHENG_URL);

        List<NameValuePair> params = new ArrayList<>();
        // 语言 目前只支持zh固定值
        params.add(new BasicNameValuePair("lan", "zh"));
        // 客户端类型选择 web端写1
        params.add(new BasicNameValuePair("ctp", "1"));
        // cuid 用户唯一标识,用来计算UV值。建议填写能区分用户的机器 MAC 地址或 IMEI 码,长度为60字符以内
        params.add(new BasicNameValuePair("cuid", "kelvinylon"));
        // token
        params.add(new BasicNameValuePair("tok", token));
        // text 语音文本
        params.add(new BasicNameValuePair("tex", encodeText));
        // 以下是选填项
        // vol 音量 0-15
        if (vol != null) {
            params.add(new BasicNameValuePair("vol", vol));
        }

        // per 发音人 0、1、3、4
        if (per != null) {
            params.add(new BasicNameValuePair("per", per));
        }

        // spd 语速 0-15
        if (spd != null) {
            params.add(new BasicNameValuePair("spd", spd));
        }
        
        // pit 语调
        if(pit != null) {
            params.add(new BasicNameValuePair("pit", pit));
        }

        // aue 3为mp3格式(默认); 4为pcm-16k;5为pcm-8k;6为wav(内容同pcm-16k);
        // 注意aue=4或者6是语音识别要求的格式,但是音频内容不是语音识别要求的自然人发音,所以识别效果会受影响。
        if (aue != null) {
            params.add(new BasicNameValuePair("aue", aue));
        }

        UrlEncodedFormEntity entity;
        try {
            entity = new UrlEncodedFormEntity(params);
        } catch (UnsupportedEncodingException e1) {
            throw new RuntimeException(e1);
        }
        post.setEntity(entity);

        RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000)
                .setSocketTimeout(20000).build();
//        get.setConfig(config);
        post.setConfig(config);
        try {
            CloseableHttpResponse resp = httpClient.execute(post);
            HttpEntity respEntity = resp.getEntity();
            String contentType = respEntity.getContentType().getValue();
            System.out.println(contentType);
            if (contentType.equals("application/json")) {
                throw new RuntimeException("错误:contentType为json,此请求的contentType应为音频文件");
            }
            InputStream contentIn = respEntity.getContent();
            File outFile = new File(outputFile);
            if(!outFile.getParentFile().exists()) {
                boolean b = outFile.getParentFile().mkdirs();
                if(!b) {
                    throw new RuntimeException("创建父文件夹失败");
                }
            }
            
            FileOutputStream fout = new FileOutputStream(outputFile);
            try {
                IOUtils.copy(contentIn, fout);
            } finally {
                IOUtils.closeQuietly(contentIn);
                IOUtils.closeQuietly(fout);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void generateYuYin(String token, String text, String outputFile) {
        generateYuYin(token, text, null, null, null, null, null, outputFile);
    }

    /**
     * 语音识别成文字
     * 
     * @param in
     * @param token
     * @return
     */
    public String reconizeYuYin(InputStream in, String token) {
        return null;
    }

}
 

http://www.lryc.cn/news/264454.html

相关文章:

  • nodejs连接mongodb报错SyntaxError: Unexpected token .
  • Ubuntu 常用命令之 gunzip 命令用法介绍
  • sun.misc.BASE64Encoder 进行maven打包时报错
  • [DNS网络] 网页无法打开、显示不全、加载卡顿缓慢 | 解决方案
  • CSS设计器的使用
  • 3d渲染太慢怎么办?2024效果图云渲染AI加速来袭
  • 指针函数函数指针回调函数相关知识
  • 软件设计模式:六大设计原则
  • Unity闪屏Logo去除
  • Git账户密码http方式的配置
  • 【JUC】三十二、邮戳锁StampedLock
  • 城市里的“蛋壳运动空间”
  • Linux宝塔面板本地部署Discuz论坛发布到公网访问【无需公网IP】
  • Android Canvas状态save与restore,Kotlin
  • python爬取网页图片并下载
  • 亚马逊prime会员日活动是免费的吗?prime day怎么选产品促销?——站斧浏览器
  • 二叉树题目:输出二叉树
  • apache poi_5.2.5 实现对表格单元格的自定义变量名进行图片替换
  • Kafka--Kafka日志索引详解以及生产常见问题分析与总结
  • Vue3-23-组件-依赖注入的使用详解
  • css 美化滚动条
  • Tomcat介绍及使用:构建强大的Java Web应用服务器
  • 怎么定义一套完成标准的JAVA枚举类型
  • Apache Seatunnel本地源码构建编译运行调试
  • 构建高效持久层:深度解析 MyBatis-Plus(02)
  • Gitlab仓库推送到Gitee仓库的一种思路
  • 快速能访问服务器的文件
  • Diary26-Vue综合案例1-书籍购物车
  • 【EasyExcel实践】万能导出,一个接口导出多张表以及任意字段(可指定字段顺序)-简化升级版
  • 解决 Hive 外部表分隔符问题的实用指南