.html?spm=a2c4g.11186623.6.574.1e1672194f1IFG
1.获取accessToken
1.1管理端(阿里云智能语音交互)获取(容易失效)
1.2 代码获取
依赖文件如下:
<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>3.7.1</version>
</dependency><!--注意fastjson包是否重复引入 -->
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.49</version>
</dependency>
具体获取accessToken代码:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import ptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;SimpleDateFormat;
import java.util.Date;public class CreateTokenDemo {// 地域IDprivate static final String REGIONID = "cn-shanghai";// 获取Token服务域名private static final String DOMAIN = "nls-meta-shanghai.aliyuncs";// API版本private static final String API_VERSION = "2019-02-28";// API名称private static final String REQUEST_ACTION = "CreateToken";// 响应参数private static final String KEY_TOKEN = "Token";private static final String KEY_ID = "Id";private static final String KEY_EXPIRETIME = "ExpireTime";public static void main(String args[]) throws ClientException {// 改用自己账号的accessKeyId和accessKeySecret!!!String accessKeyId = "2QWEAFS231qewga24qw";String accessKeySecret = "21q3wrfar2q23WQDSWawf2qqewd2";// 创建DefaultAcsClient实例并初始化DefaultProfile profile = Profile(REGIONID,accessKeyId,accessKeySecret);IAcsClient client = new DefaultAcsClient(profile);CommonRequest request = new CommonRequest();request.setDomain(DOMAIN);request.setVersion(API_VERSION);request.setAction(REQUEST_ACTION);request.setMethod(MethodType.POST);request.setProtocol(ProtocolType.HTTPS);CommonResponse response = CommonResponse(request);System.out.Data());if (HttpStatus() == 200) {JSONObject result = JSON.Data());String token = JSONObject(KEY_TOKEN).getString(KEY_ID);long expireTime = JSONObject(KEY_TOKEN).getLongValue(KEY_EXPIRETIME);System.out.println("获取到的Token: " + token + ",有效期时间戳(单位:秒): " + expireTime);// 将10位数的时间戳转换为北京时间String expireDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(expireTime * 1000));System.out.println("Token有效期的北京时间:" + expireDate);}else {System.out.println("获取Token失败!");}}
}
2.直接使用RESTFUL API 方式调用
依赖如下:
<dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>3.9.1</version>
</dependency><!-- .alibaba/fastjson -->
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.42</version>
</dependency>
具体语音转文字代码:
import com.alibaba.fastjson.JSONPath;
import com.voicecomm.socket.utils.HttpUtil;import java.util.HashMap;public class SpeechRecognizerRESTfulDemo {private String accessToken;private String appkey;public SpeechRecognizerRESTfulDemo(String appkey, String token) {this.appkey = appkey;this.accessToken = token;}public void process(String fileName, String format, int sampleRate,boolean enablePunctuationPrediction,boolean enableInverseTextNormalization,boolean enableVoiceDetection) {/*** 设置HTTP RESTful POST请求:* 1.使用HTTP协议。* 2.语音识别服务域名:nls-gateway-shanghai.aliyuncs。* 3.语音识别接口请求路径:/stream/v1/asr。* 4.设置必选请求参数:appkey、format、sample_rate。* 5.设置可选请求参数:enable_punctuation_prediction、enable_inverse_text_normalization、enable_voice_detection。*/String url = "";String request = url;request = request + "?appkey=" + appkey;request = request + "&format=" + format;request = request + "&sample_rate=" + sampleRate;if (enablePunctuationPrediction) {request = request + "&enable_punctuation_prediction=" + true;}if (enableInverseTextNormalization) {request = request + "&enable_inverse_text_normalization=" + true;}if (enableVoiceDetection) {request = request + "&enable_voice_detection=" + true;}System.out.println("Request: " + request);/*** 设置HTTP头部字段:* 1.鉴权参数。* 2.Content-Type:application/octet-stream。*/HashMap<String, String> headers = new HashMap<String, String>();headers.put("X-NLS-Token", this.accessToken);headers.put("Content-Type", "application/octet-stream");/*** 发送HTTP POST请求,返回服务端的响应。*/String response = HttpUtil.sendPostFile(request, headers, fileName);if (response != null) {System.out.println("Response: " + response);String result = ad(response, "result").toString();System.out.println("识别结果:" + result);} else {println("识别失败!");}}public static void main(String[] args) {// 改用自己账号的appkey和获取的最新accessToken !!!String token = "22fcdb54bb29456cbd3e9c86625f304b";String appkey = "DWQ12dwe132fas2f";SpeechRecognizerRESTfulDemo demo = new SpeechRecognizerRESTfulDemo(appkey, token);// String fileName = ClassLoader().getResource("./nls-sample-16k.wav").getPath();// 录音文件全路径,一定是全路径!!!不然会报"The filePath is not a file:"String fileName = "C:\Users\Administrator\Downloads\demo_8k\wav\020390496.wav";String format = "pcm";// 看你的录音是8k还是16k!!!int sampleRate = 8000;boolean enablePunctuationPrediction = true;boolean enableInverseTextNormalization = true;boolean enableVoiceDetection = false;demo.process(fileName, format, sampleRate, enablePunctuationPrediction, enableInverseTextNormalization, enableVoiceDetection);}
}
HttpUtil:
import okhttp3.*;
import java.io.File;
import java.io.IOException;
import java.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;
import urrent.TimeUnit;public class HttpUtil {private static String getResponseWithTimeout(Request q) {String ret = null;OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();OkHttpClient client = tTimeout(10, TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS).writeTimeout(60, TimeUnit.SECONDS).build();try {Response s = wCall(q).execute();ret = s.body().string();s.close();} catch (SocketTimeoutException e) {ret = println("get result timeout");} catch (IOException e) {println("get result error " + e.getMessage());}return ret;}public static String sendPostFile(String url, HashMap<String, String> headers, String fileName) {RequestBody body;File file = new File(fileName);if (!file.isFile()) {println("The filePath is not a file: " + fileName);return null;} else {body = ate(MediaType.parse("application/octet-stream"), file);}Headers.Builder hb = new Headers.Builder();if (headers != null && !headers.isEmpty()) {for (Map.Entry<String, String> entry : Set()) {hb.Key(), Value());}}Request request = new Request.Builder().url(url).headers(hb.build()).post(body).build();return getResponseWithTimeout(request);}public static String sendPostData(String url, HashMap<String, String> headers, byte[] data) {RequestBody body;if (data.length == 0) {println("The send data is empty.");return null;} else {body = ate(MediaType.parse("application/octet-stream"), data);}Headers.Builder hb = new Headers.Builder();if (headers != null && !headers.isEmpty()) {for (Map.Entry<String, String> entry : Set()) {hb.Key(), Value());}}Request request = new Request.Builder().url(url).headers(hb.build()).post(body).build();return getResponseWithTimeout(request);}
}
完事!!!
代码可直接拿去运行,只需要改动相关参数,文中相应位置做了’!!!'标记
阿里云智能语音交互管理端配置
本文发布于:2024-01-30 20:00:00,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170661600422468.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |