Files
FreeERP.Applet/miniprogram/utils/voiceUtil.ts

31 lines
797 B
TypeScript
Raw Normal View History

2025-12-26 17:26:32 +08:00
import { post, urlAddBaseUrl } from "./https";
const playVoice = (src: string, text: string) => {
2025-12-26 17:26:32 +08:00
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: string, cacheVoice?: boolean) => {
2025-12-26 17:26:32 +08:00
post(
"/Tools/voice",
{ txt: text, per: 3, spd: 5, pit: 5, vol: 9 },
2025-12-26 17:26:32 +08:00
{ showLoading: false }
).then((res: any) => {
2025-12-26 17:26:32 +08:00
if (res.err_code == 0) {
playVoice(urlAddBaseUrl(res.data), text);
}
});
};