// components/music/music.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        audioSrc: {
            type: String
        },
        audioName: {
            type: String
        },
        audioTitle: {
            type: String
        },
    },
    /**
     * 组件的初始数据
     */
    data: {
        audioPlay: false, //当前的播放状态控制
        sliderValue: 0, //进度条最小值
        sliderMax: 100, //进度条最大值
        backgroundAudioManager: "", //播放实例
        currentTimeStr: "00:00", //当前进度的时间
        durationTimeStr: "00:00", //总的时间
        changeValState: false //在拉动进度条的时候,拉动完成的状态
    },
    /**
     * 组件的方法列表
     */
    methods: {
        creatAudio: function () {
            let that = this;
            that.data.backgroundAudioManager = wx.getBackgroundAudioManager();
            that.data.backgroundAudioManager.title = '音频';
            that.data.backgroundAudioManager.onPlay(function () { //播放监听
                console.log('播放!');
                that.setData({
                    audioPlay: true
                })
            })
            that.data.backgroundAudioManager.onPause(function () { //暂停监听
                console.log('暂停播放!');
                that.setData({
                    audioPlay: false
                })
            })
            that.data.backgroundAudioManager.onEnded(function () { //结束播放监听
                console.log('播放结束!');
                that.setData({
                    sliderValue:0,
                    audioPlay: false
                })
            })
            that.data.backgroundAudioManager.onError(function (res) { // 播放失败监听
                console.log('播放音频失败', res);
            })
            that.data.backgroundAudioManager.onSeeked(function (res) { //监听结束跳转事件callback(无效)
                console.log("结束跳转", res);
                that.setData({
                    changeValState: false
                })
            })
            that.data.backgroundAudioManager.onTimeUpdate(function (res) { //监听背景音频播放进度更新事件
                if (that.data.changeValState) return false;
                let currentTime = that.data.backgroundAudioManager.currentTime;
                let duration = that.data.backgroundAudioManager.duration;
                if (that.data.sliderMax == 100) {
                    that.setData({
                        sliderMax: duration.toFixed(0)
                    })
                }
                let val = parseInt((currentTime / duration) * that.data.sliderMax);
                that.setData({
                    sliderValue: val
                })
                let currTimeStr = that.formatTime(currentTime);
                let duraTimeStr = that.formatTime(duration);
                that.setData({
                    currentTimeStr: currTimeStr,
                    durationTimeStr: duraTimeStr
                })
            })

        },
        //滑动条改变事件
        sliderChange: function (e) {
            let that = this;
            console.log(e);
            that.setData({
                sliderValue: e.detail.value
            })
            if (that.data.backgroundAudioManager == "") return false;
            that.setData({
                changeValState: true
            })
            that.data.backgroundAudioManager.seek(e.detail.value);
            setTimeout(function () {
                that.setData({
                    changeValState: false
                })
            }, 500)
        },
        //播放按钮点击事件
        changePlayState: function () {
            let that = this;
            this.setData({
                audioPlay: !this.data.audioPlay
            })
            if (this.data.audioPlay) {
                if (!that.data.backgroundAudioManager.src) { //初始化给backgroundAudioManager.src赋值
                    that.creatAudio();
                    that.data.backgroundAudioManager.src = that.data.audioSrc; //当设置了src后会自动播放
                }
                that.data.backgroundAudioManager.play();
            } else {
                that.data.backgroundAudioManager.pause();
            }
        },
        formatTime: function (num) { //格式化时间格式
            num = num.toFixed(0);
            let second = (num % 60);
            if (second < 10) second = '0' + second;
            let min = Math.floor(num / 60);
            if (min < 10) min = '0' + min;
            return min + ":" + second;
        }
    }
})