UNPKG

@awayjs/core

Version:
210 lines (209 loc) 7.64 kB
import { __extends } from "tslib"; import { ParserUtils } from '../parsers/ParserUtils'; import { BaseAudioChannel } from './BaseAudioChannel'; var EventAudioChannel = /** @class */ (function (_super) { __extends(EventAudioChannel, _super); function EventAudioChannel(groupID, groupVolume, groupPan) { if (groupID === void 0) { groupID = 0; } if (groupVolume === void 0) { groupVolume = 1; } if (groupPan === void 0) { groupPan = 1; } var _this = _super.call(this) || this; _this._groupID = 0; _this._groupVolume = 1; _this._groupPan = 0; _this._startTime = 0; _this._groupID = groupID; _this._groupVolume = groupVolume; _this._groupPan = groupPan; _this._audio = new Audio(); _this._audio.ontimeupdate = function (event) { return _this._onTimeUpdate(event); }; return _this; } EventAudioChannel.stopAllSounds = function (channelGroup) { if (channelGroup === void 0) { channelGroup = -1; } var len = EventAudioChannel._channels.length; if (channelGroup < 0) { for (var j = 0; j < len; j++) { EventAudioChannel._channels[j].stop(); } EventAudioChannel._channels.length = 0; return; } var aliveChannels = []; for (var j = 0; j < len; j++) { if (EventAudioChannel._channels[j].groupID == channelGroup) { EventAudioChannel._channels[j].stop(); } else { aliveChannels[aliveChannels.length] = EventAudioChannel._channels[j]; } } EventAudioChannel._channels = aliveChannels; }; EventAudioChannel.setChannelGroupVolume = function (value, channelGroup) { if (channelGroup === void 0) { channelGroup = -1; } var len = EventAudioChannel._channels.length; if (channelGroup < 0) { for (var j = 0; j < len; j++) { EventAudioChannel._channels[j].groupVolume = value; } return; } for (var j = 0; j < len; j++) { if (EventAudioChannel._channels[j].groupID == channelGroup) { EventAudioChannel._channels[j].groupVolume = value; } } }; Object.defineProperty(EventAudioChannel.prototype, "duration", { get: function () { return this._duration; }, enumerable: false, configurable: true }); Object.defineProperty(EventAudioChannel.prototype, "currentTime", { get: function () { return this._audio.currentTime - this._startTime; }, enumerable: false, configurable: true }); Object.defineProperty(EventAudioChannel.prototype, "pan", { get: function () { //todo return 0; }, set: function (value) { //todo }, enumerable: false, configurable: true }); Object.defineProperty(EventAudioChannel.prototype, "groupID", { get: function () { return this._groupID; }, set: function (value) { this._groupID = value; }, enumerable: false, configurable: true }); Object.defineProperty(EventAudioChannel.prototype, "groupVolume", { get: function () { return this._groupVolume; }, set: function (value) { if (this._groupVolume == value) return; this._groupVolume = value; this._audio.volume = this._groupVolume * this._volume; }, enumerable: false, configurable: true }); Object.defineProperty(EventAudioChannel.prototype, "groupPan", { get: function () { return this._groupPan; }, set: function (value) { }, enumerable: false, configurable: true }); Object.defineProperty(EventAudioChannel.prototype, "volume", { get: function () { return this._volume; }, set: function (value) { if (this._volume == value) return; this._volume = value; this._audio.volume = this._volume; }, enumerable: false, configurable: true }); EventAudioChannel.prototype.isPlaying = function () { return this._isPlaying; }; EventAudioChannel.prototype.isLooping = function () { return this._isLooping; }; EventAudioChannel.prototype.isDecoding = function () { return false; }; EventAudioChannel.prototype.restart = function () { this._isPlaying = false; if (this._stopped) { throw 'You can\'t restart channel that was fully stopped'; } if (!this._audio) { return false; } this._isPlaying = true; this._audio.play(); this.dispatchRestart(); return true; }; EventAudioChannel.prototype.play = function (buffer, offset, loop, id) { var _this = this; if (offset === void 0) { offset = 0; } if (loop === void 0) { loop = false; } if (id === void 0) { id = 0; } _super.prototype.play.call(this, buffer, offset, loop, id); this._isPlaying = true; this._isLooping = this._loops > 0; this._audio.src = EventAudioChannel._base64Cache[id] || (EventAudioChannel._base64Cache[id] = ParserUtils.arrayBufferToBase64(buffer, 'audio/mp3')); //this._audio.loop = this._isLooping; var thisAudio = this._audio; this._audio.addEventListener('loadedmetadata', function () { thisAudio.currentTime = offset; thisAudio.play(); }, false); this._audio.addEventListener('error', function (err) { console.log('error in audio', err); _this.dispatchStop(true); }, false); this._audio.addEventListener('canplay', function (err) { console.log('canplay in audio', err); }, false); this._audio.addEventListener('canplaythrough', function (err) { console.log('canplaythrough in audio', err); }, false); this._audio.addEventListener('abort', function (err) { console.log('abort in audio', err); _this.dispatchStop(true); }, false); this._audio.addEventListener('loadstart', function (err) { console.log('loadstart in audio', err); }, false); this._audio.addEventListener('suspend', function (err) { console.log('suspend in audio', err); }, false); }; EventAudioChannel.prototype.stopInternally = function (emitComplete) { if (emitComplete === void 0) { emitComplete = false; } this._audio.pause(); this._isPlaying = false; this._isLooping = false; _super.prototype.completeInternally.call(this, emitComplete, emitComplete); }; EventAudioChannel.prototype.stop = function () { this.stopInternally(false); this.dispatchStop(false); }; EventAudioChannel.prototype._onTimeUpdate = function (event) { //TODO: more accurate end detection if (!this._isLooping && this._audio.duration < this._audio.currentTime - this._startTime + 0.1) { this.stopInternally(true); } }; EventAudioChannel.maxChannels = 4; EventAudioChannel._channels = new Array(); EventAudioChannel._base64Cache = {}; return EventAudioChannel; }(BaseAudioChannel)); export { EventAudioChannel };