Show:

File: libs/Media.js

var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
/**
 * @module annie
 */
var annie;
(function (annie) {
    /**
     * <h4><font color="red">小游戏不支持 小程序不支持</font></h4>
     * 抽象类 一般不直接使用
     * @class annie.Media
     * @extends annie.EventDispatcher
     * @public
     * @since 1.0.0
     */
    var Media = /** @class */ (function (_super) {
        __extends(Media, _super);
        /**
         * 构造函数
         * @method Media
         * @param {string|HtmlElement} src
         * @param {string} type
         * @since 1.0.0
         * @example
         *      var media = new annie.Media('http://test.annie2x.com/biglong/apiDemo/annieBitmap/resource/music.mp3', 'Audio');
         *          media.play();//媒体播放
         *          //media.pause();//暂停播放
         *          //media.stop();//停止播放
         */
        function Media(src, type) {
            var _this = _super.call(this) || this;
            /**
             * html 标签 有可能是audio 或者 video
             * @property media
             * @type {Video|Audio}
             * @public
             * @since 1.0.0
             */
            _this.media = null;
            /**
             * 媒体类型 VIDEO 或者 AUDIO
             * @property type
             * @type {string}
             * @since 1.0.0
             */
            _this.type = "";
            /**
             * @property isPlaying
             * @type {boolean}
             * @since 2.0.0
             * @default true
             */
            _this.isPlaying = true;
            /**
             * 给一个声音取一个名字,方便获取
             * @property name
             * @type {string}
             */
            _this.name = "";
            _this._loop = 1;
            /**
             * @property _repeate
             * @type {number}
             * @private
             * @default 1
             */
            _this._repeate = 1;
            var s = _this;
            s._instanceType = "annie.Media";
            if (typeof (src) == "string") {
                s.media = document.createElement(type);
                s.media.src = src;
            }
            else {
                s.media = src;
            }
            s._SBWeixin = s._weixinSB.bind(s);
            s.media.addEventListener('ended', s._endEvent = function () {
                if (s._loop == -1) {
                    s.play(0);
                }
                else {
                    s._loop--;
                    if (s._loop > 0) {
                        s.play(0, s._loop);
                    }
                    else {
                        s.stop();
                    }
                }
                s.dispatchEvent("onPlayEnd");
            }.bind(s));
            s.type = type.toLocaleUpperCase();
            s.media.addEventListener("timeupdate", s._updateEvent = function () {
                s.dispatchEvent("onPlayUpdate", { currentTime: s.media.currentTime });
            });
            s.media.addEventListener("play", s._playEvent = function () {
                s.dispatchEvent("onPlayStart");
            });
            return _this;
        }
        /**
         * 开始播放媒体
         * @method play
         * @param {number} start 开始点 默认为0
         * @param {number} loop 循环次数 默认为1
         * @public
         * @since 1.0.0
         */
        Media.prototype.play = function (start, loop) {
            if (start === void 0) { start = 0; }
            if (loop === void 0) { loop = 0; }
            var s = this;
            if (loop == 0) {
                s._loop = s._repeate;
            }
            else {
                s._loop = loop;
                s._repeate = loop;
            }
            if (s.media.currentTime != start) {
                s.media.currentTime = start;
            }
            //马蛋的有些ios微信无法自动播放,需要做一些特殊处理
            var wsb = window;
            if (wsb.WeixinJSBridge) {
                try {
                    wsb.WeixinJSBridge.invoke("getNetworkType", {}, s._SBWeixin);
                }
                catch (e) {
                    s.media.play();
                }
            }
            else {
                s.media.play();
            }
            s.isPlaying = true;
        };
        Media.prototype._weixinSB = function () {
            this.media.play();
        };
        /**
         * 停止播放
         * @method stop
         * @public
         * @since 1.0.0
         */
        Media.prototype.stop = function () {
            var s = this;
            s.media.pause();
            s.media.currentTime = 0;
            s.isPlaying = false;
        };
        /**
         * 暂停播放,或者恢复播放
         * @method pause
         * @public
         * @param isPause  默认为true;是否要暂停,如果要暂停,则暂停;否则则播放
         * @since 1.0.4
         */
        Media.prototype.pause = function (isPause) {
            if (isPause === void 0) { isPause = true; }
            var s = this;
            if (isPause) {
                s.media.pause();
                s.isPlaying = false;
            }
            else {
                s.media.play();
                s.isPlaying = true;
            }
        };
        Object.defineProperty(Media.prototype, "volume", {
            /**
             * 设置或者获取音量 从0-1
             * @since 1.1.0
             * @property volume
             * @return {number}
             */
            get: function () {
                return this.media.volume;
            },
            set: function (value) {
                var s = this;
                s.media.volume = value;
                if (value == 0) {
                    s.media.muted = true;
                }
                else {
                    s.media.muted = false;
                }
            },
            enumerable: true,
            configurable: true
        });
        Media.prototype.destroy = function () {
            var s = this;
            s.media.pause();
            s.media.removeEventListener("ended", s._endEvent);
            s.media.removeEventListener("onPlayStart", s._playEvent);
            s.media.removeEventListener("timeupdate", s._updateEvent);
            s.media = null;
            _super.prototype.destroy.call(this);
        };
        return Media;
    }(annie.EventDispatcher));
    annie.Media = Media;
})(annie || (annie = {}));