import { rejects } from "assert";
import { Howl, HowlCallback, Howler } from "howler";

import { resolve } from "path";
import { ref, watch, ComputedRef } from "vue";
import { useStore } from "vuex";
export function useAudio() {
    const store = useStore()
    const token = "Bearer " + store.getters.getToken;
    Howler.autoUnlock = true
    Howler.usingWebAudio = true
    let sound: Howl = store.state.lightAudio
    let pauseAudio = () => {
        sound && sound.pause();
    }
    let stopAudio = () => {
        sound && sound.stop()

    }
    let playAudio = (url: string) => {
        pauseAudio()
        sound = new Howl({
            src: url,
            preload:true,
            autoplay: true
        });
        return new Promise((resolve, reject) => {
            sound.once('unlock', () => {
                alert("我解锁了")
            })
            sound.once('playerror', (err, code) => {
                alert(err + ",播放失败" + code)
            })
            sound.on('loaderror', (err, code) => {
                alert(err + "加载失败" + code)
            })
            sound.once("load", function () {
                let id = sound.play();
                if (id) {

                    resolve(id)
                }
                else {
                    reject("id错误")
                }

            });

        })
    }
    let audioOnce = (event: string, callback: HowlCallback) => {
        sound.once(event, callback)

    }
    let audioOn = (event: string, callback: HowlCallback) => {
        sound.on(event, callback)

    }
    let playMulAudio = (urls: string[]) => {
        pauseAudio()
        sound = new Howl({
            src: urls
        });

    }
    let durationAudio = (id?: number) => {
        return sound && sound.duration(id)

    }





    return {
        playAudio,
        audioOnce,
        audioOn,
        playMulAudio,
        pauseAudio,
        stopAudio,
        durationAudio

    }


}