You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.5 KiB
JavaScript
54 lines
1.5 KiB
JavaScript
import config from "@/config";
|
|
export const recognizeAudio = async (tempFilePath) => {
|
|
try {
|
|
const fileInfo = await new Promise((resolve, reject) => {
|
|
uni.getFileInfo({
|
|
filePath: tempFilePath,
|
|
success: resolve,
|
|
fail: reject,
|
|
});
|
|
});
|
|
const uploadRes = await new Promise((resolve, reject) => {
|
|
uni.uploadFile({
|
|
url: `http://106.227.91.181:9022/recognize_speech`,
|
|
filePath: tempFilePath,
|
|
name: "speech",
|
|
formData: {
|
|
format: "amr",
|
|
rate: 16000,
|
|
channel: 1,
|
|
cuid: "uniapp_user",
|
|
audio_len: fileInfo.size,
|
|
},
|
|
success: (res) => {
|
|
if (res.statusCode === 200) {
|
|
try {
|
|
const data = JSON.parse(res.data);
|
|
resolve({ statusCode: 200, data });
|
|
} catch (e) {
|
|
reject(new Error("响应解析失败: " + e.message));
|
|
}
|
|
} else {
|
|
reject(new Error(`上传失败: ${res.statusCode}`));
|
|
}
|
|
},
|
|
fail: (err) => reject(new Error("上传请求失败: " + err.errMsg)),
|
|
});
|
|
});
|
|
|
|
const result = uploadRes.data;
|
|
if (result.status === "success") {
|
|
return result.result;
|
|
} else {
|
|
throw new Error(result.error || "识别失败");
|
|
}
|
|
} catch (error) {
|
|
console.error("语音识别错误:", error);
|
|
uni.showToast({
|
|
title: "识别失败: " + (error.message || "网络错误"),
|
|
icon: "none",
|
|
});
|
|
return null;
|
|
}
|
|
};
|