/**************************************************************************** Copyright (c) 2008-2010 Ricardo Quesada Copyright (c) 2011-2012 cocos2d-x.org Copyright (c) 2013-2016 Chukong Technologies Inc. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ var EventTarget = require('../core/event/event-target'); var sys = require('../core/platform/CCSys'); var LoadMode = require('../core/assets/CCAudioClip').LoadMode; var touchBinded = false; var touchPlayList = [ //{ instance: Audio, offset: 0, audio: audio } ]; var Audio = function (src) { EventTarget.call(this); this._src = src; this._element = null; this.id = 0; this._volume = 1; this._loop = false; this._nextTime = 0; // playback position to set this._state = Audio.State.INITIALZING; this._onended = function () { this.emit('ended'); }.bind(this); }; cc.js.extend(Audio, EventTarget); /** * !#en Audio state. * !#zh 声音播放状态 * @enum audioEngine.AudioState * @memberof cc */ // TODO - At present, the state is mixed with two states of users and systems, and it is best to split into two types. A "loading" should also be added to the system state. Audio.State = { /** * @property {Number} ERROR */ ERROR : -1, /** * @property {Number} INITIALZING */ INITIALZING: 0, /** * @property {Number} PLAYING */ PLAYING: 1, /** * @property {Number} PAUSED */ PAUSED: 2 }; (function (proto) { proto._bindEnded = function (callback) { callback = callback || this._onended; if (this._src && this._src.loadMode === LoadMode.DOM_AUDIO) { this._element.addEventListener('ended', callback); } else { this._element.onended = callback; } }; proto._unbindEnded = function () { if (this._src && this._src.loadMode === LoadMode.DOM_AUDIO) { this._element.removeEventListener('ended', this._onended); } else { this._element.onended = null; } }; // proto.mount = function (elem) { // if (CC_DEBUG) { // cc.warn('Audio.mount(value) is deprecated. Please use Audio._onLoaded().'); // } // }; proto._onLoaded = function () { var elem = this._src._nativeAsset; if (CC_QQPLAY || CC_WECHATGAME) { this._element = elem; } else { if (this._src.loadMode === LoadMode.DOM_AUDIO) { this._element = document.createElement('audio'); this._element.src = elem.src; } else { this._element = new WebAudioElement(elem, this); } } this.setVolume(this._volume); this.setLoop(this._loop); if (this._nextTime !== 0) { this.setCurrentTime(this._nextTime); } if (this._state === Audio.State.PLAYING) { this.play(); } else { this._state = Audio.State.INITIALZING; } }; proto.play = function () { // marked as playing so it will playOnLoad this._state = Audio.State.PLAYING; if (!this._element) { return; } this._bindEnded(); this._element.play(); if (!(CC_QQPLAY || CC_WECHATGAME) && this._src && this._src.loadMode === LoadMode.DOM_AUDIO && this._element.paused) { touchPlayList.push({ instance: this, offset: 0, audio: this._element }); } if (touchBinded) return; touchBinded = true; // Listen to the touchstart body event and play the audio when necessary. cc.game.canvas.addEventListener('touchstart', function () { var item; while (item = touchPlayList.pop()) { item.audio.play(item.offset); } }); }; proto.destroy = function () { }; proto.pause = function () { if (!this._element) return; this._unbindEnded(); this._element.pause(); this._state = Audio.State.PAUSED; }; proto.resume = function () { if (!this._element || this._element.currentTime === 0) return; this._bindEnded(); this._element.play(); this._state = Audio.State.PLAYING; }; proto.stop = function () { if (!this._element) return; try { this._element.currentTime = 0; } catch (error) {} this._element.pause(); // remove touchPlayList for (var i=0; i Audio duration return this._context.currentTime - this._startTime > this._buffer.duration; }); proto.__defineGetter__('loop', function () { return this._loop; }); proto.__defineSetter__('loop', function (bool) { if (this._currentSource) this._currentSource.loop = bool; return this._loop = bool; }); proto.__defineGetter__('volume', function () { return this._volume; }); proto.__defineSetter__('volume', function (num) { this._volume = num; if (this._gainObj['gain'].setTargetAtTime) { this._gainObj['gain'].setTargetAtTime(this._volume, this._context.currentTime, 0.01); } else { this._volume['gain'].value = num; } if (sys.os === sys.OS_IOS && !this.paused && this._currentSource) { // IOS must be stop webAudio this._currentSource.onended = null; this.pause(); this.play(); } return num; }); proto.__defineGetter__('currentTime', function () { if (this.paused) { return this.playedLength; } // Record the time the current has been played this.playedLength = this._context.currentTime - this._startTime; // If more than the duration of the audio, Need to take the remainder this.playedLength %= this._buffer.duration; return this.playedLength; }); proto.__defineSetter__('currentTime', function (num) { if (!this.paused) { this.pause(); this.playedLength = num; this.play(); } else { this.playedLength = num; } return num; }); proto.__defineGetter__('duration', function () { return this._buffer.duration; }); })(WebAudioElement.prototype); module.exports = cc.Audio = Audio;