科大讯飞 语音合成


    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 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";


        MapparamMap =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 parameter, String charset,
							  String refer,Map 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地址,否则视为不合法访问请求。

科大讯飞 语音合成_第1张图片

你可能感兴趣的:(科大讯飞 语音合成)