科大讯飞 语音合成


    static String apiKey="apikey";
    static String xparamOrgin="{ \"auf\":\"audio/L16;rate=16000\",\"aue\": \"lame\",\"voice_name\":\"xiaoyan\",\"speed\":\"50\",\"volume\":\"50\",\"pitch\":\"50\",\"engine_type\":\"intp65\",\"text_type\":\"text\"}";
    static String xparam = Base64.encoder().encodeToString(xparamOrgin.getBytes());

    public static void main(String[]args){

        System.out.println(post("hello"));

    }


    public static String post(String txt){
        String currentTimeMillis =System.currentTimeMillis()/1000L+"";
        String checkSum =null;
        try {
            checkSum=md5(apiKey+currentTimeMillis+xparam);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }


        Map<String,String> headermap =new HashMap<>();
        headermap.put("X-Appid","appId");
        headermap.put("X-CurTime",currentTimeMillis);
        headermap.put("X-Param",xparam);
        headermap.put("X-CheckSum",checkSum);
        headermap.put("Content-Type","application/x-www-form-urlencoded; charset=utf-8");

        String url="http://api.xfyun.cn/v1/service/v1/tts";


        Map<String,Object>paramMap =new HashMap<>();
        try {
          //  paramMap.put("text", URLEncoder.encode(txt, "utf-8"));
            paramMap.put("text", txt);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            post(url,paramMap,null,null,headermap,"");
        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }



    public static String md5(String data) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(data.getBytes());
        StringBuffer buf = new StringBuffer();
        byte[] bits = md.digest();
        for(int i=0;i<bits.length;i++){
            int a = bits[i];
            if(a<0) a+=256;
            if(a<16) buf.append("0");
            buf.append(Integer.toHexString(a));
        }
        return buf.toString();
    }














	/**
	 * post 方法
	 *
	 * @param targetUrl
	 *            目标地址
	 * @param parameter
	 *            parameter里面的值可以为File,File[],String,String[]
	 * @param charset 编码
	 * @param refer 来源
	 * @return
	 * @throws Exception
	 */
	public static String post(String targetUrl, Map<String, Object> parameter, String charset,
							  String refer,Map<String, String> headerMap,String path) throws Exception
	{
		URI uri = new URI(targetUrl);
		CloseableHttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore)
				.setDefaultHeaders(createHeaders(uri, refer)).setRetryHandler(retryHandler)
				.addInterceptorFirst(gzipInterceptor).setRedirectStrategy(new DefaultRedirectStrategy()).build();
		String res = null;
		HttpPost post = new HttpPost(uri);

		try
		{
			// 处理表单中提交的参数
			HttpEntity entity;
			Charset textCharset = charset == null ? Charset.forName(encoding) : Charset.forName(charset);
			for (String headerName : headerMap.keySet()) {
				if ("Content-Length".equalsIgnoreCase(headerName)) {
					continue;
				}
				String headerValue = headerMap.get(headerName);
				post.addHeader(headerName, headerValue);
			}
			entity = new UrlEncodedFormEntity(doCommPart(parameter), textCharset);
			post.setEntity(entity);

			 dealOutPutMp3Response(client.execute(post),path);
		}
		finally
		{
			post.releaseConnection();
			client.close();
		}
		return null;
	}





	/**
	 * 处理HttpResponse内容,解决可能的乱码问题
	 *
	 * @param response
	 * @return
	 * @throws Exception
	 *
	 */
	private static void dealOutPutMp3Response(HttpResponse response,String path) throws Exception
	{
		HttpEntity entity = response.getEntity();// unCompressEntity(response);
		ContentType contentType = ContentType.getOrDefault(entity);

		InputStream in = entity.getContent();

		BufferedInputStream reader = new BufferedInputStream(in);
		OutputStream outs = new FileOutputStream(path);
		int size = 0, len = 0;
		byte[] buf = new byte[1024];
		while ((size = reader.read(buf)) != -1) {
			len += size;
			outs.write(buf, 0, size);
		}
		outs.close();
		reader.close();
		in.close();
		EntityUtils.consume(entity);
	}






apiKey   appID均在控制台 项目里

IP白名单 必须添加本机的外网ip地址,否则视为不合法访问请求。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值