import { post, urlAddBaseUrl } from "./https";

const playVoice = (src: string, text: string) => {
  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) => {
  post(
    "/Tools/voice",
    { txt: text, per: 3, spd: 5, pit: 5, vol: 9 },
    { showLoading: false }
  ).then((res: any) => {
    if (res.err_code == 0) {
      playVoice(urlAddBaseUrl(res.data), text);
    }
  });
};
