window.h5Audio = (function () {

    if (typeof Object.assign !== 'function') {
        // Must be writable: true, enumerable: false, configurable: true
        Object.defineProperty(Object, "assign", {
            value: function assign(target, varArgs) { // .length of function is 2
                'use strict';
                if (target === null || target === undefined) {
                    throw new TypeError('Cannot convert undefined or null to object');
                }

                var to = Object(target);

                for (var index = 1; index < arguments.length; index++) {
                    var nextSource = arguments[index];

                    if (nextSource !== null && nextSource !== undefined) {
                        for (var nextKey in nextSource) {
                            // Avoid bugs when hasOwnProperty is shadowed
                            if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
                                to[nextKey] = nextSource[nextKey];
                            }
                        }
                    }
                }
                return to;
            },
            writable: true,
            configurable: true
        });
    }

    var e = "data:image/gif;base64, ",
        o = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFQAAABUCAMAAAArteDzAAAAaVBMVEUAAAAarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRkarRlIa6J1AAAAInRSTlMA9wYa38QR7ZJnMK1IIqBsO3fXDbSGQudZz5fKpV0rfbpRlHIjYQAAA35JREFUWMPFWduyqjAMDS0tgtwEFBGv/P9Hntmh3cWDTYsMs/Oio3SRy0qapuCU7PXIRdUGQxCFncgfrwzWCb/l4TCTML/xbxFlIQariEJ+AZnkwUBKkCdLIZvBQ5olsPw61Uhc4vTOa4Ca39P4IqYWXH2dyw5mWXUs2ez/8liZVx6YD2bW6wXRzmpesov0U70HxW5azTBmpD1xqJW9uUzfaS0Lp1ms0Nru6Nfv9WPSi8lahT2BKoWyvARPKZUPhLRiduq9ckHaKds6y5pa6XmARXJQutaEP4MzLJTzyJfmk193I2YKiyUdUXcf+OnCdKPO+JqNvxO2kx4YNcr+c2jvjpE7Wv27W4uRS/C1jFEu3mpdhJyX34PWISY3ByNj/SxhhZRjfZ0UMkUJt3Bxx08rJU2xbFB16YEZDiG3JSy6sHlXNPbCHIbOVpHiN1VzjBLzKOCkmxjGKld6B4oNbjkiqi3rkJeBNN8jBj7SUEaxyGgnjE1OkS0mHkUAgd5X/qWF80mWR7PaOY0410GrnHHXVHpSqlZII521RzeXqtpkTkgEEitIiwF1YeLDJgQnIldbgAx5wMBj5z4br+aWB5GdGbxUxGjUp6ESLmxhJsaMFzx+Pi5+VIpN6bTUlcvPfw/InXlvjO5MjsdE/ucg6DjxRlEJY4Wb0J1IlnR0ZoXGEHF/6l1I68d+vj3ho9xH0mO+cjumNiMxvg/tTOWYcIAkqCl+XjRbtH7CHv4aCQrIQIui3TCxNPyN1BMXfhQFFxCgJ/yzmYAaTpGgEZpPoOq60GJctfkRaX5IBApRVTNTm/TvnYHqCEoh6kMzUCuNxnUUpVzkB/2+/Pc5iTpT5PdNUx78FrMT6kymqbugmEpxNZU4JXaph7v0GbOGxJQ3SZU+ryINSWT8iAt6skg7txPD1wCJN/rrQG0nZuNzo54nHQOnNj6zRTtRj5Pe5klu0d7NBGTThvFENhNE20NQS5BtD9GgUdQqyQZtaSuZ4bIr1fUGcmHTCz1SRpJNL9GeE3xNHe35/CDhRj04DhLzI48b9eI48mxxONvyGLn+wGtsLTY5mm87RFg/7jhNxh3bD2aANWtHSFsOu7Yfy60fIG4/6lw/lN14fOwedJdWXxKD7m1H8u7LAwZMZsn88mCDa46/v5DZ6OoIhcf7dg7Y7mPalb7XcVEwDEFU+V3H/QOplcP+ctPpgwAAAABJRU5ErkJggg==";

    var a = function (i) {
        var t = {
            ele: null,
            width: "320px",
            title: "这是一个测试title",
            src: "",
            disc: "这是一个测试disc",
            autoplay: false,
            loop: true,
            ended: function () {
            }
        };
        this.opt = Object.assign({}, t, i),
        "string" === typeof this.opt.ele && (this.opt.ele = document.querySelector(this.opt.ele)),
        this.opt.ele && (this.loading = !1,
            this.isDrag = !1,
            this.isplaying = !1,
            this.durationT = 0,
            this.currentT = 0,
            this.currentP = 0,
            this.maxProgressWidth = 0,
            this.dragProgressTo = 0,
            this.reduceTBefore = 0,
            this.reduceTAfter = 0,
            this.initDom());
        return this;
    };

    a.prototype = {
        // constructor:function(i) {
        //
        // },

        initDom: function () {
            this.wxAudioC = document.createElement("div"),
                this.wxAudioC.className = "wx-audio-content",
                this.wxAudioC.style.width = this.opt.width,
                this.opt.ele.appendChild(this.wxAudioC),
                this.wxAudio = document.createElement("audio"),
                this.wxAudio.className = "wx-audio-content",
                this.wxAudio.src = this.opt.src,
            this.opt.loop && this.wxAudio.setAttribute("loop", this.opt.loop),
                this.wxAudioC.appendChild(this.wxAudio),
                this.wxAudioL = document.createElement("div"),
                this.wxAudioL.className = "wx-audio-left",
                this.wxAudioC.appendChild(this.wxAudioL),
                this.wxAudioStateImg = document.createElement("img"),
                this.wxAudioStateImg.className = "wx-audio-state",
                this.wxAudioStateImg.src = o,
                this.wxAudioL.appendChild(this.wxAudioStateImg),
                this.wxAudioR = document.createElement("div"),
                this.wxAudioR.className = "wx-audio-right",
                this.wxAudioC.appendChild(this.wxAudioR),
                this.wxAudioT = document.createElement("p"),
                this.wxAudioT.className = "wx-audio-title",
                this.wxAudioT.innerText = this.opt.title,
                this.wxAudioR.appendChild(this.wxAudioT),
                this.wxAudioD = document.createElement("p"),
                this.wxAudioD.className = "wx-audio-disc",
                this.wxAudioD.innerText = this.opt.disc,
                this.wxAudioR.appendChild(this.wxAudioD),
                this.wxAudioP = document.createElement("div"),
                this.wxAudioP.className = "wx-audio-progrees",
                this.wxAudioR.appendChild(this.wxAudioP),
                this.wxAudioDetail = document.createElement("div"),
                this.wxAudioDetail.className = "wx-progrees-detail",
                this.wxAudioP.appendChild(this.wxAudioDetail),
                this.wxVoiceP = document.createElement("span"),
                this.wxVoiceP.className = "wx-voice-p",
                this.wxAudioDetail.appendChild(this.wxVoiceP),
                this.wxBufferP = document.createElement("span"),
                this.wxBufferP.className = "wx-buffer-p",
                this.wxAudioDetail.appendChild(this.wxBufferP),
                this.wxLoading = document.createElement("span"),
                this.wxLoading.className = "wx-loading",
                this.wxAudioDetail.appendChild(this.wxLoading),
                this.wxLoadingWrapper = document.createElement("span"),
                this.wxLoadingWrapper.className = "wx-loading-wrapper",
                this.wxLoading.appendChild(this.wxLoadingWrapper),
                this.wxAudioOrigin = document.createElement("div"),
                this.wxAudioOrigin.className = "wx-audio-origin",
                this.wxAudioP.appendChild(this.wxAudioOrigin),
                this.wxAudioTime = document.createElement("div"),
                this.wxAudioTime.className = "wx-audio-time",
                this.wxAudioR.appendChild(this.wxAudioTime),
                this.wxAudioCurrent = document.createElement("span"),
                this.wxAudioCurrent.className = "current-t",
                this.wxAudioCurrent.innerText = "00:00",
                this.wxAudioTime.appendChild(this.wxAudioCurrent),
                this.wxAudioDuration = document.createElement("span"),
                this.wxAudioDuration.className = "duration-t",
                this.wxAudioDuration.innerText = "00:00",
                this.wxAudioTime.appendChild(this.wxAudioDuration),
                this.initAudioEvent();
            if (this.opt.autoplay) {
                var that = this;
                document.addEventListener("WeixinJSBridgeReady", function () {
                    alert(1);
                    that.audioPlay();
                });
                window.addEventListener("load", function () {
                    that.audioPlay();
                });
            }
        },

        audioPlay: function () {
            this.wxAudio.play(),
                this.isPlaying = !0
        },
        audioPause: function () {
            this.wxAudio.pause(),
                this.isPlaying = !1
        },
        audioPlayPause: function () {
            this.isPlaying ? this.audioPause() : this.audioPlay()
        },
        audioCut: function (i, t, A) {
            this.wxAudio.src = i,
                this.wxAudioT.innerText = t,
                this.wxAudioD.innerText = A,
                this.durationT = 0,
                this.currentT = 0,
                this.currentP = 0,
                this.dragProgressTo = 0,
                this.wxAudioCurrent.innerText = "00:00",
                this.wxAudioOrigin.style.left = "0px",
                this.wxVoiceP.style.width = "0px",
                this.audioPlay()
        },
        showLoading: function (i) {
            this.loading = i || !1,
                this.loading ? this.wxLoading.style.display = "block" : this.wxLoading.style.display = "none"
        },
        initAudioEvent: function () {
            var i = this;
            i.wxAudio.onplaying = function () {
                var t = new Date;
                i.isPlaying = !0,
                    i.reduceTBefore = Date.parse(t) - Math.floor(1e3 * i.wxAudio.currentTime),
                    i.wxAudioStateImg.src = e
            }
                ,
                i.wxAudio.onpause = function () {
                    i.isPlaying = !1,
                        i.showLoading(!1),
                        i.wxAudioStateImg.src = o
                }
                ,
                i.wxAudio.onloadedmetadata = function () {
                    i.durationT = i.wxAudio.duration,
                        i.wxAudioDuration.innerText = i.formartTime(i.wxAudio.duration)
                }
                ,
                i.wxAudio.onwaiting = function () {
                    i.wxAudio.paused || i.showLoading(!0)
                }
                ,
                i.wxAudio.onprogress = function () {
                    if (i.wxAudio.buffered.length > 0) {
                        for (var t = 0, A = 0; A < i.wxAudio.buffered.length; A++)
                            t += i.wxAudio.buffered.end(A) - i.wxAudio.buffered.start(A),
                            t > i.durationT && (t = i.durationT,
                                i.showLoading(!1),
                                console.log("缓冲完成"));
                        var e = Math.floor(t / i.durationT * 100);
                        i.wxBufferP.style.width = e + "%"
                    }
                    var o = new Date;
                    i.wxAudio.paused || (i.reduceTAfter = Date.parse(o) - Math.floor(1e3 * i.currentT),
                        i.reduceTAfter - i.reduceTBefore > 1e3 ? i.showLoading(!0) : i.showLoading(!1))
                }
                ,
                i.wxAudio.onended = function () {
                    i.opt.ended()
                }
                ,
                i.wxAudio.ontimeupdate = function () {
                    var t = new Date;
                    i.isDrag || (i.currentT = i.wxAudio.currentTime,
                        i.currentP = Number(i.wxAudio.currentTime / i.durationT * 100),
                        i.reduceTBefore = Date.parse(t) - Math.floor(1e3 * i.currentT),
                        i.currentP = i.currentP > 100 ? 100 : i.currentP,
                        i.wxVoiceP.style.width = i.currentP + "%",
                        i.wxAudioOrigin.style.left = i.currentP + "%",
                        i.wxAudioCurrent.innerText = i.formartTime(i.wxAudio.currentTime),
                        i.showLoading(!1))
                }
                ,
                i.wxAudioStateImg.onclick = function () {
                    i.audioPlayPause()
                }
                ,
                i.wxAudioOrigin.onmousedown = function (t) {
                    i.isDrag = !0;
                    var A = t || window.event
                        , e = A.clientX
                        , o = t.target.offsetLeft;
                    i.maxProgressWidth = i.wxAudioDetail.offsetWidth,
                        i.wxAudioC.onmousemove = function (t) {
                            if (i.isDrag) {
                                var A = t || window.event
                                    , a = A.clientX;
                                i.dragProgressTo = Math.min(i.maxProgressWidth, Math.max(0, o + (a - e))),
                                    i.updatePorgress()
                            }
                        }
                        ,
                        i.wxAudioC.onmouseup = function () {
                            i.isDrag && (i.isDrag = !1,
                                i.wxAudio.currentTime = Math.floor(i.dragProgressTo / i.maxProgressWidth * i.durationT))
                        }
                        ,
                        i.wxAudioC.onmouseleave = function () {
                            i.isDrag && (i.isDrag = !1,
                                i.wxAudio.currentTime = Math.floor(i.dragProgressTo / i.maxProgressWidth * i.durationT))
                        }
                }
                ,
                i.wxAudioOrigin.ontouchstart = function (t) {
                    i.isDrag = !0;
                    var A = t || window.event
                        , e = A.touches[0].clientX
                        , o = A.target.offsetLeft;
                    i.maxProgressWidth = i.wxAudioDetail.offsetWidth,
                        i.wxAudioC.ontouchmove = function (t) {
                            if (i.isDrag) {
                                var A = t || window.event
                                    , a = A.touches[0].clientX;
                                i.dragProgressTo = Math.min(i.maxProgressWidth, Math.max(0, o + (a - e))),
                                    i.updatePorgress()
                            }
                        }
                        ,
                        i.wxAudioC.ontouchend = function () {
                            i.isDrag && (i.isDrag = !1,
                                i.wxAudio.currentTime = Math.floor(i.dragProgressTo / i.maxProgressWidth * i.durationT))
                        }
                }
                ,
                i.wxAudioDetail.onclick = function (t) {
                    var A = t || window.event
                        , e = A.layerX
                        , o = i.wxAudioDetail.offsetWidth;
                    i.wxAudio.currentTime = Math.floor(e / o * i.durationT)
                }
        },
        isWeiXin: function () {
            var i = window.navigator.userAgent.toLowerCase();
            return "micromessenger" === String(i.match(/MicroMessenger/i))
        },
        updatePorgress: function () {
            this.wxAudioOrigin.style.left = this.dragProgressTo + "px",
                this.wxVoiceP.style.width = this.dragProgressTo + "px";
            var i = Math.floor(this.dragProgressTo / this.maxProgressWidth * this.durationT);
            this.wxAudioCurrent.innerText = this.formartTime(i)
        },
        formartTime: function (i) {
            var t = function (i) {
                return i = i.toString(),
                    i[1] ? i : "0" + i
            }
                , A = Math.floor(i / 60)
                , e = Math.floor(i % 60);
            return t(A) + ":" + t(e)
        }
    };

    return a;
})();