# nls
**Repository Path**: elfbobo/nls
## Basic Information
- **Project Name**: nls
- **Description**: 阿里云语音服务
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 3
- **Created**: 2019-08-20
- **Last Updated**: 2020-12-18
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
## 简介
借助于阿里云语音服务,实现的自动将语音转化为文本
## 使用
#### 1. pom.xml 的 dependencies 中加入
````
com.xnx3.util
xnx3-util
1.0.0
com.alibaba.nls
nls-sdk-long-asr
2.0.3
com.alibaba.nls
nls-sdk-short-asr
2.0.3
com.alibaba.nls
nls-sdk-tts
2.0.3
com.aliyun
aliyun-java-sdk-core
3.7.1
com.alibaba
fastjson
1.2.49
````
#### 2. 代码使用示例
````
package com.xnx3.nls;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.xnx3.BaseVO;
import com.xnx3.nls.aliyun.NlsUtil;
/**
* 智能对话
* @author 管雷鸣
*/
@Controller
@RequestMapping("/chat")
public class TestController{
static{
/*
* 先设置初始化的参数。这个在程序中只需设置一次即可。
* accessKeyId 阿里云 AccessKeyID
* accessKeySecret 阿里云AccessKeySecret
* appKey 语音识别 appkey
*/
NlsUtil.init("LTA.............", "dTuDCHVnZF....................", "JbhVJ2..........");
}
/**
* 语音问答
* @param voice 用户说出的话,语音
* @return baseVO.info 回复的内容
* @throws IOException
*/
@RequestMapping(value="/voiceTell.do", method = RequestMethod.POST)
@ResponseBody
public BaseVO voiceTell(HttpServletRequest request, HttpServletResponse response,
@RequestParam("voice") MultipartFile voice) throws IOException{
//允许跨域请求
response.addHeader("Access-Control-Allow-Origin", "*");
//说出的声音转换为的文字
String voiceText = NlsUtil.voiceToString(voice.getInputStream());
return BaseVO.success(voiceText);
}
}
````