阿里云语音识别 一句话识别
RESTful API
根据文档封装微信小程序正常使用h5需要配置跨域
/**
* 阿里云语音识别-一句话识别 restful Api
*
* @param {filePath} 音频临时oss地址
* @param {appkey} 服务端返回的阿里云中的appkey
* @param {token} 服务端返回的阿里云中的token
* @return {string|boolean} 成功返回文件地址,失败返回false
*/
function audioToText(option = {
token,
filePath,
appkey,
format
}, callBackSuccess, callBackFail) {
let url = 'https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/asr';
let sampleRate = '16000';
let enablePunctuationPrediction = true;
let enableInverseTextNormalization = true;
let enableVoiceDetection = false;
/**
* 设置RESTful请求参数
*/
var requestUrl = url;
requestUrl = requestUrl + '?appkey=' + option.appkey;
requestUrl = requestUrl + '&format=' + option.format;
requestUrl = requestUrl + '&sample_rate=' + sampleRate;
if (enablePunctuationPrediction) {
requestUrl = requestUrl + '&enable_punctuation_prediction=' + 'true';
}
if (enableInverseTextNormalization) {
requestUrl = requestUrl + '&enable_inverse_text_normalization=' + 'true';
}
if (enableVoiceDetection) {
requestUrl = requestUrl + '&enable_voice_detection=' + 'true';
}
requestUrl = requestUrl + '&audio_address=' + option.filePath;
uni.showLoading({
title: '处理中',
mask: true
});
uni.request({
url: requestUrl,
method: 'POST',
header: {
'X-NLS-Token': option.token,
'content-type': 'application/octet-stream',
},
data: {},
success: (res) => {
if (res.data.status == 20000000) {
callBackSuccess(res.data);
} else {
showNetError();
callBackFail(res);
}
},
fail: (res) => {
showNetError();
callBackFail(res);
},
complete: () => {
uni.hideLoading();
}
})
}
页面引用
import audioAli from "@/utils/audio.js"
使用
audioAli.audioToText({
token: "服务端获取token",
filePath,//音频oss地址 可通过公网访问的音频文件下载链接。
appkey: "服务端获取",
format: 'mp3',
}, res => {
// res.result 语音转文字的值
}, err => {})