Files
FreeERP.Applet/miniprogram/utils/voiceUtil.js
2025-12-26 17:26:32 +08:00

76 lines
1.9 KiB
JavaScript

import { post, urlAddBaseUrl } from "./https";
const playVoice = (src, text) => {
const innerAudioContext = wx.createInnerAudioContext({
useWebAudioImplement: true,
});
innerAudioContext.src = src;
innerAudioContext.play();
innerAudioContext.onError(() => {
innerAudioContext.destroy();
console.log("Audio Error");
// wx.removeStorageSync(text);
});
innerAudioContext.onEnded(() => {
innerAudioContext.destroy();
console.log("Audio End");
});
};
export const voiceRequest = (text, cacheVoice) => {
// if (cacheVoice) {
// const data = wx.getStorageSync(text);
// console.log(data);
// if (data) {
// console.log("有 base64");
// const fs = wx.getFileSystemManager();
// //随机定义路径名称
// var times = new Date().getTime();
// var codeimg = wx.env.USER_DATA_PATH + "/" + times + ".mp3";
// //将base64图片写入
// fs.writeFile({
// filePath: codeimg,
// data: data,
// encoding: "base64",
// success: (res) => {
// console.log(res);
// playVoice(codeimg, text);
// },
// fail: (res) => {
// console.log(res);
// },
// });
// return;
// }
// }
post(
"/Tools/voice",
{
txt: text,
per: 3,
spd: 5,
pit: 5,
vol: 9,
},
{ showLoading: false }
).then((res) => {
if (res.err_code == 0) {
playVoice(urlAddBaseUrl(res.data), text);
// if (cacheVoice) {
// wx.request({
// url: urlAddBaseUrl(res.data),
// responseType: "arraybuffer",
// success(res) {
// const base64 = base64js.fromByteArray(new Uint8Array(res.data));
// try {
// wx.setStorageSync(text, base64);
// } catch (_) {
// console.log("缓存错误");
// }
// },
// });
// }
}
});
};