web-audio-api-player
Advanced tools
Comparing version 5.0.1 to 5.0.2
@@ -1109,3 +1109,2 @@ /****************************************************************************** | ||
PlayerCore.prototype._findBestSource = function (soundSource) { | ||
var _this = this; | ||
var bestSource = { | ||
@@ -1122,6 +1121,8 @@ url: null, | ||
} | ||
sources.forEach(function (source) { | ||
var i = 0; | ||
while (i < sources.length) { | ||
var source = sources[i]; | ||
var soundUrl = ''; | ||
if (_this._options.soundsBaseUrl !== '') { | ||
soundUrl = _this._options.soundsBaseUrl; | ||
if (this._options.soundsBaseUrl !== '') { | ||
soundUrl = this._options.soundsBaseUrl; | ||
} | ||
@@ -1131,8 +1132,9 @@ soundUrl += source.url; | ||
if (source.codec !== null) { | ||
isCodecSupported = _this._checkCodecSupport(source.codec); | ||
isCodecSupported = this._checkCodecSupport(source.codec); | ||
} | ||
if (isCodecSupported) | ||
if (bestSource.url !== null && source.isPreferred) { | ||
if (isCodecSupported) { | ||
if (source.isPreferred) { | ||
bestSource.url = soundUrl; | ||
bestSource.codec = source.codec; | ||
break; | ||
} | ||
@@ -1143,3 +1145,5 @@ else { | ||
} | ||
}); | ||
} | ||
i++; | ||
} | ||
return bestSource; | ||
@@ -1146,0 +1150,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
var e=function(t,o){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])},e(t,o)};function t(e,t,o,n){return new(o||(o=Promise))((function(i,u){function r(e){try{a(n.next(e))}catch(e){u(e)}}function s(e){try{a(n.throw(e))}catch(e){u(e)}}function a(e){var t;e.done?i(e.value):(t=e.value,t instanceof o?t:new o((function(e){e(t)}))).then(r,s)}a((n=n.apply(e,t||[])).next())}))}function o(e,t){var o,n,i,u,r={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return u={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function s(s){return function(a){return function(s){if(o)throw new TypeError("Generator is already executing.");for(;u&&(u=0,s[0]&&(r=0)),r;)try{if(o=1,n&&(i=2&s[0]?n.return:s[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,s[1])).done)return i;switch(n=0,i&&(s=[2&s[0],i.value]),s[0]){case 0:case 1:i=s;break;case 4:return r.label++,{value:s[1],done:!1};case 5:r.label++,n=s[1],s=[0];continue;case 7:s=r.ops.pop(),r.trys.pop();continue;default:if(!(i=r.trys,(i=i.length>0&&i[i.length-1])||6!==s[0]&&2!==s[0])){r=0;continue}if(3===s[0]&&(!i||s[1]>i[0]&&s[1]<i[3])){r.label=s[1];break}if(6===s[0]&&r.label<i[1]){r.label=i[1],i=s;break}if(i&&r.label<i[2]){r.label=i[2],r.ops.push(s);break}i[2]&&r.ops.pop(),r.trys.pop();continue}s=t.call(e,r)}catch(e){s=[6,e],n=0}finally{o=i=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,a])}}}var n=function(){function e(e){this.url=null,this.codec=null,this.sourceNode=null,this.gainNode=null,this.isReadyToPLay=!1,this.isBuffered=!1,this.isBuffering=!1,this.audioElement=null,this.audioBuffer=null,this.arrayBuffer=null,this.audioBufferDate=null,this.playTimeOffset=0,this.startTime=0,this.playTime=0,this.playedTimePercentage=0,this.state="sound_state_stopped",this.loadingProgress=0,this.duration=null,this.firstTimePlayed=!0,Array.isArray(e.source)?this.source=e.source:this.source=[e.source],void 0!==e.id?this.id=e.id:this.id=this._generateSoundId(),this.loop=e.loop||!1,this.duration=e.duration||null,"function"==typeof e.onLoading?this.onLoading=e.onLoading:this.onLoading=null,"function"==typeof e.onPlaying?this.onPlaying=e.onPlaying:this.onPlaying=null,"function"==typeof e.onStarted?this.onStarted=e.onStarted:this.onStarted=null,"function"==typeof e.onEnded?this.onEnded=e.onEnded:this.onEnded=null,"function"==typeof e.onStopped?this.onStopped=e.onStopped:this.onStopped=null,"function"==typeof e.onPaused?this.onPaused=e.onPaused:this.onPaused=null,"function"==typeof e.onResumed?this.onResumed=e.onResumed:this.onResumed=null,e.arrayBuffer instanceof ArrayBuffer&&(this.arrayBuffer=e.arrayBuffer),e.audioBuffer instanceof AudioBuffer&&(this.audioBuffer=e.audioBuffer,this.isBuffering=!1,this.isBuffered=!0,this.audioBufferDate=new Date,this.duration=this.getDuration())}return e.prototype.getCurrentTime=function(){var e;return null!==this.sourceNode&&(this.sourceNode instanceof AudioBufferSourceNode?e=this.sourceNode.context.currentTime:this.sourceNode instanceof MediaElementAudioSourceNode&&(e=this.audioElement.currentTime)),e},e.prototype.getDuration=function(){var e;return null!==this.sourceNode&&(this.sourceNode instanceof AudioBufferSourceNode?e=this.sourceNode.buffer.duration:this.sourceNode instanceof MediaElementAudioSourceNode&&(e=this.audioElement.duration)),e},e.prototype._generateSoundId=function(){return Date.now().toString(36)+Math.random().toString(36).substring(2)},e.SOUND_STATE_STOPPED="sound_state_stopped",e.SOUND_STATE_PAUSED="sound_state_paused",e.SOUND_STATE_PLAYING="sound_state_playing",e}(),i=function(t){function o(e,n){var i=t.call(this,e)||this;return i.code=n||null,Object.setPrototypeOf(i,o.prototype),i}return function(t,o){if("function"!=typeof o&&null!==o)throw new TypeError("Class extends value "+String(o)+" is not a constructor or null");function n(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}(o,t),o}(Error),u=function(){function e(e){this._audioContext=null,this._volume=null,this._audioNodes={gainNode:null},this._options=e,this._initialize()}return e.prototype._initialize=function(){this._options.createAudioContextOnFirstUserInteraction&&this._addAutoCreateAudioContextOnFirstUserInteractionEventListeners()},e.prototype.decodeAudio=function(e){return t(this,void 0,void 0,(function(){var t;return o(this,(function(o){switch(o.label){case 0:return[4,this.getAudioContext()];case 1:return[4,o.sent().decodeAudioData(e)];case 2:return t=o.sent(),[2,Promise.resolve(t)]}}))}))},e.prototype._createAudioContext=function(){var e=this;return new Promise((function(t,o){e._audioContext instanceof AudioContext&&t();var n=window.AudioContext||window.webkitAudioContext;try{null!==e._options.audioContext?e._audioContext=e._options.audioContext:e._audioContext=new n,t()}catch(e){o(e)}}))},e.prototype._addAutoCreateAudioContextOnFirstUserInteractionEventListeners=function(){this._options.createAudioContextOnFirstUserInteraction&&(document.addEventListener("touchstart",this.getAudioContext.bind(this)),document.addEventListener("touchend",this.getAudioContext.bind(this)),document.addEventListener("mousedown",this.getAudioContext.bind(this)))},e.prototype._removeAutoCreateAudioContextOnFirstUserInteractionEventListeners=function(){this._options.createAudioContextOnFirstUserInteraction&&(document.removeEventListener("touchstart",this.getAudioContext.bind(this)),document.removeEventListener("touchend",this.getAudioContext.bind(this)),document.removeEventListener("mousedown",this.getAudioContext.bind(this)))},e.prototype.getAudioContext=function(){return t(this,void 0,void 0,(function(){return o(this,(function(e){switch(e.label){case 0:return null!==this._audioContext?[3,2]:[4,this._createAudioContext()];case 1:return e.sent(),[3,4];case 2:return"suspended"!==this._audioContext.state?[3,4]:[4,this._unfreezeAudioContext()];case 3:e.sent(),e.label=4;case 4:return[2,this._audioContext]}}))}))},e.prototype._unfreezeAudioContext=function(){return void 0===this._audioContext.suspend?Promise.resolve():this._audioContext.resume()},e.prototype.freezeAudioContext=function(){return void 0===this._audioContext.suspend?Promise.resolve():this._audioContext.suspend()},e.prototype.detectAudioContextSupport=function(){var e=!1;return(void 0!==window.webkitAudioContext||"undefined"!=typeof AudioContext)&&(e=!0),e},e.prototype.detectAudioElementSupport=function(){return!!document.createElement("audio").canPlayType},e.prototype.shutDown=function(e){return t(this,void 0,void 0,(function(){return o(this,(function(t){switch(t.label){case 0:return this._removeAutoCreateAudioContextOnFirstUserInteractionEventListeners(),e.forEach((function(e){e.sourceNode instanceof MediaElementAudioSourceNode?void 0!==e.sourceNode.mediaElement&&e.sourceNode.mediaElement.remove():(e.sourceNode,AudioBufferSourceNode)})),this._disconnectPlayerGainNode(),[4,this._destroyAudioContext()];case 1:return t.sent(),[2]}}))}))},e.prototype._destroyAudioContext=function(){return t(this,void 0,void 0,(function(){return o(this,(function(e){switch(e.label){case 0:return null===this._audioContext?[3,2]:[4,this._audioContext.close()];case 1:e.sent(),this._audioContext=null,e.label=2;case 2:return[2]}}))}))},e.prototype.createAudioBufferSourceNode=function(e,n){return t(this,void 0,void 0,(function(){var t,i;return o(this,(function(o){switch(o.label){case 0:return[4,this.getAudioContext()];case 1:return t=o.sent(),i=t.createBufferSource(),n.sourceNode=i,i.loop=e.loop,n.gainNode=i.context.createGain(),n.gainNode.gain.value=1,i.connect(n.gainNode),i.onended=function(t){e.onSourceNodeEnded(t)},[2]}}))}))},e.prototype.createMediaElementSourceNode=function(e,n){return t(this,void 0,void 0,(function(){var u,r,s=this;return o(this,(function(a){switch(a.label){case 0:return null!==n.sourceNode?[3,2]:[4,this.getAudioContext()];case 1:r=a.sent();try{u=r.createMediaElementSource(e.mediaElement)}catch(e){throw new i(e)}u.mediaElement.loop=e.loop,n.gainNode=u.context.createGain(),n.gainNode.gain.value=1,u.connect(n.gainNode),u.mediaElement.onended=function(){return t(s,void 0,void 0,(function(){return o(this,(function(t){return e.onSourceNodeEnded(),[2]}))}))},n.sourceNode=u,a.label=2;case 2:return[2]}}))}))},e.prototype._getPlayerGainNode=function(){return t(this,void 0,void 0,(function(){var e,t;return o(this,(function(o){switch(o.label){case 0:return this._audioNodes.gainNode instanceof GainNode?(e=this._audioNodes.gainNode,[3,3]):[3,1];case 1:return[4,this.getAudioContext()];case 2:t=o.sent(),(e=t.createGain()).connect(t.destination),this._audioNodes.gainNode=e,o.label=3;case 3:return this._initializeVolume(),[2,e]}}))}))},e.prototype._disconnectPlayerGainNode=function(){this._audioNodes.gainNode.disconnect(),this._audioNodes.gainNode=null},e.prototype.connectSound=function(e){return t(this,void 0,void 0,(function(){var t,n;return o(this,(function(o){switch(o.label){case 0:return[4,this._getPlayerGainNode()];case 1:return t=o.sent(),null!==(n=e.gainNode)&&n.connect(t),[2]}}))}))},e.prototype.disconnectSound=function(e){return t(this,void 0,void 0,(function(){return o(this,(function(t){if(null===e.gainNode)throw new i("can't destroy as no source node in sound");return e.sourceNode instanceof AudioBufferSourceNode&&(e.sourceNode=null),[2]}))}))},e.prototype._changePlayerGainValue=function(e){this._audioNodes.gainNode instanceof GainNode&&(this._audioNodes.gainNode.gain.value=e)},e.prototype._roundGainTwoDecimals=function(e){return Math.round(100*(e+Number.EPSILON))/100},e.prototype.setVolume=function(e,t){void 0===t&&(t=!0),this._options.persistVolume&&t&&localStorage.setItem("WebAudioAPIPlayerVolume",e.toString());var o=e/100;this._audioNodes.gainNode instanceof GainNode&&(this._roundGainTwoDecimals(this._audioNodes.gainNode.gain.value)!==this._volume&&this._changePlayerGainValue(o));this._volume=e},e.prototype.getVolume=function(){var e;if(null!==this._volume)e=this._volume;else{if(this._options.persistVolume){var t=parseInt(localStorage.getItem("WebAudioAPIPlayerVolume"));isNaN(t)||(e=t)}void 0===e&&(e=this._options.volume)}return e},e.prototype._initializeVolume=function(){if(this._options.persistVolume){var e=parseInt(localStorage.getItem("WebAudioAPIPlayerVolume"));isNaN(e)||this.setVolume(e,!1)}null===this._volume&&this.setVolume(this._options.volume,!1)},e}(),r=function(){function e(){}return e.prototype.getArrayBuffer=function(e){return new Promise((function(t,o){var n=new XMLHttpRequest;n.open("GET",e.url,!0),n.responseType="arraybuffer",n.onload=function(){200===n.status?t(n.response):o(new i(n.statusText,n.status))},n.onprogress=function(t){var o=100/(t.total/t.loaded);e.loadingProgress=o,null!==e.onLoading&&e.onLoading(o,t.total,t.loaded)},n.onerror=function(){o(new i("xhr network error"))},n.send()}))},e}(),s=function(){function e(e){var t=this;void 0===e&&(e={}),this._playingProgressRequestId=null,this._playingProgressPreviousTimestamp=0,this._progressTrigger=function(e,o){o-t._playingProgressPreviousTimestamp>=t._options.playingProgressIntervalTime&&(t._playingProgress(e),t._playingProgressPreviousTimestamp=o),t._playingProgressRequestId=window.requestAnimationFrame((function(o){t._progressTrigger(e,o)}))};var o={volume:80,loopQueue:!1,loopSong:!1,soundsBaseUrl:"",playingProgressIntervalTime:200,playNextOnEnded:!0,stopOnReset:!0,visibilityAutoMute:!1,createAudioContextOnFirstUserInteraction:!0,persistVolume:!0,loadPlayerMode:"player_mode_audio",audioContext:null},n=Object.assign({},o,e);this._queue=[],this._currentIndex=null,this._options=n,this._initialize()}return e.prototype._initialize=function(){var t=this._audioOptions();switch(this._playerAudio=new u(t),this._options.loadPlayerMode){case e.PLAYER_MODE_AUDIO:if(!this._playerAudio.detectAudioContextSupport())throw new i("audio context is not supported by this device");if(!this._playerAudio.detectAudioElementSupport())throw new i("audio element is not supported by this device");break;case e.PLAYER_MODE_AJAX:if(!this._playerAudio.detectAudioContextSupport())throw new i("audio context is not supported by this device")}},e.prototype._audioOptions=function(){return{audioContext:this._options.audioContext,createAudioContextOnFirstUserInteraction:this._options.createAudioContextOnFirstUserInteraction,volume:this._options.volume,persistVolume:this._options.persistVolume}},e.prototype.addSoundToQueue=function(t){var o=t.soundAttributes,i=t.whereInQueue,u=void 0===i?"append":i,r=new n(o);switch(u){case e.WHERE_IN_QUEUE_AT_END:this._appendSoundToQueue(r);break;case e.WHERE_IN_QUEUE_AT_START:this._prependSoundToQueue(r)}return r},e.prototype._appendSoundToQueue=function(e){this._queue.push(e)},e.prototype._prependSoundToQueue=function(e){this._queue.unshift(e)},e.prototype.resetQueue=function(){this._options.stopOnReset&&this.stop(),this._queue=[]},e.prototype.reset=function(){this.resetQueue()},e.prototype.getQueue=function(){return this._queue},e.prototype.setVolume=function(e){this._playerAudio.setVolume(e)},e.prototype.getVolume=function(){return this._playerAudio.getVolume()},e.prototype.setLoopQueue=function(e){this._options.loopQueue=e},e.prototype.getLoopQueue=function(){return this._options.loopQueue},e.prototype.mute=function(){var e=this.getVolume();this._postMuteVolume=e,this._playerAudio.setVolume(0,!1)},e.prototype.unMute=function(){this._playerAudio.setVolume(this._postMuteVolume,!1),this._postMuteVolume=null},e.prototype.isMuted=function(){return null===this._postMuteVolume},e.prototype.setPosition=function(t){var o=this,n=this._getSoundFromQueue({whichSound:e.CURRENT_SOUND});if(null!==n)if(null===n.duration||isNaN(n.duration))this._loadSound(n).then((function(e){var n=e.duration/100*t;o.setPositionInSeconds(n)})).catch((function(e){throw e}));else{var i=n.duration/100*t;this.setPositionInSeconds(i)}},e.prototype.setPositionInSeconds=function(t){var o=this._getSoundFromQueue({whichSound:e.CURRENT_SOUND});if(null===o)throw new i("position change called, but no current sound found");o.state===n.SOUND_STATE_PLAYING?this.play({whichSound:o.id,playTimeOffset:t}):o.playTimeOffset=t},e.prototype._loadSound=function(t){var o,n;switch(this._options.loadPlayerMode){case e.PLAYER_MODE_AUDIO:o=this._loadSoundUsingAudioElement(t);break;case e.PLAYER_MODE_AJAX:o=this._loadSoundUsingRequest(t);break;case e.PLAYER_MODE_FETCH:n=new i(e.PLAYER_MODE_FETCH+" is not implemented yet",1),o=Promise.reject(n)}return o},e.prototype._loadSoundUsingAudioElement=function(e){var t=this;return new Promise((function(o,n){var u=t._findBestSource(e.source),r=u.url,s=u.codec,a=void 0===s?null:s;if(e.url=r,e.codec=a,e.arrayBuffer=null,null!==e.url){var d=new Audio;d.crossOrigin="anonymous",d.src=e.url,d.controls=!1,d.autoplay=!1,d.id="web_audio_api_player_sound_"+e.id.toString(),document.body.appendChild(d),e.audioElement=d,e.isReadyToPLay=!0,t._initializeAudioElementListeners(e);var c=function(){e.audioElement.removeEventListener("canplaythrough",c),isNaN(d.duration)||(e.duration=d.duration),o(e)};e.audioElement.addEventListener("canplaythrough",c);var l=function(){e.audioElement.removeEventListener("error",l);var t=new i("loading sound failed");n(t)};e.audioElement.addEventListener("error",l)}else{var p=new i("sound has no url",1);n(p)}}))},e.prototype._loadSoundUsingRequest=function(e){var t=this;return new Promise((function(o,n){var u=t._findBestSource(e.source),s=u.url,a=u.codec,d=void 0===a?null:a;if(e.url=s,e.codec=d,null!==e.url){var c=new r;e.isBuffering=!0,c.getArrayBuffer(e).then((function(i){e.arrayBuffer=i,t._decodeSound({sound:e}).then((function(e){o(e)})).catch(n)})).catch((function(e){n(e)}))}else{var l=new i("sound has no url",1);n(l)}}))},e.prototype._initializeAudioElementListeners=function(e){e.audioElement.addEventListener("progress",(function(){e.loadingProgress=e.audioElement.duration})),e.audioElement.addEventListener("timeupdate",(function(){e.duration=e.audioElement.duration}))},e.prototype._decodeSound=function(e){var t=e.sound;return this._playerAudio.decodeAudio(t.arrayBuffer).then((function(e){return t.audioBuffer=e,t.isBuffering=!1,t.isBuffered=!0,t.audioBufferDate=new Date,t.duration=e.duration,t.isReadyToPLay=!0,t})).catch((function(e){throw e}))},e.prototype._cloneAudioBuffer=function(e){for(var t=new AudioBuffer({length:e.length,numberOfChannels:e.numberOfChannels,sampleRate:e.sampleRate}),o=0;o<t.numberOfChannels;++o){var n=e.getChannelData(o);t.copyToChannel(n,o)}return t},e.prototype.play=function(t){var o=this,i=void 0===t?{}:t,u=i.whichSound,r=i.playTimeOffset;return new Promise((function(t,i){var s=o._getSoundFromQueue({whichSound:e.CURRENT_SOUND}),a=o._getSoundFromQueue({whichSound:u,updateIndex:!0});if(null===a)throw new Error("no more sounds in array");null===s||s.state!==n.SOUND_STATE_PLAYING&&s.state!==n.SOUND_STATE_PAUSED||s.id===a.id||o._stop(s,n.SOUND_STATE_STOPPED),null!==s&&s.state===n.SOUND_STATE_PLAYING&&s.id===a.id&&o._stop(s,n.SOUND_STATE_PAUSED),null===s||null!==s&&s.id!==a.id?a.firstTimePlayed=!0:a.firstTimePlayed=!1,void 0!==r&&(a.playTimeOffset=r),a.isReadyToPLay?(null!==a.audioBuffer&&(a.audioBuffer=o._cloneAudioBuffer(a.audioBuffer)),o._play(a).then(t).catch(i)):o._loadSound(a).then((function(){o._play(a).then(t).catch(i)})).catch(i)}))},e.prototype._play=function(e){return t(this,void 0,void 0,(function(){return o(this,(function(t){switch(t.label){case 0:return null===e.audioBuffer?[3,2]:[4,this._playAudioBuffer(e)];case 1:return t.sent(),[3,4];case 2:return[4,this._playMediaElementAudio(e)];case 3:t.sent(),t.label=4;case 4:return e.state=n.SOUND_STATE_PLAYING,e.startTime=e.getCurrentTime(),e=this._triggerSoundCallbacks(e),[2]}}))}))},e.prototype._playAudioBuffer=function(e){return t(this,void 0,void 0,(function(){var t,n,u=this;return o(this,(function(o){switch(o.label){case 0:if(null!==e.sourceNode)return[3,4];t={loop:e.loop,onSourceNodeEnded:function(){u._onEnded()}},o.label=1;case 1:return o.trys.push([1,3,,4]),[4,this._playerAudio.createAudioBufferSourceNode(t,e)];case 2:return o.sent(),[3,4];case 3:throw n=o.sent(),new i(n);case 4:return e.sourceNode instanceof AudioBufferSourceNode?(e.sourceNode.buffer=e.audioBuffer,[4,this._playerAudio.connectSound(e)]):[3,6];case 5:o.sent();try{void 0!==e.playTimeOffset?e.sourceNode.start(0,e.playTimeOffset):e.sourceNode.start()}catch(e){throw new i(e)}o.label=6;case 6:return[2]}}))}))},e.prototype._playMediaElementAudio=function(e){return t(this,void 0,void 0,(function(){var t,n,u=this;return o(this,(function(o){switch(o.label){case 0:if(null!==e.sourceNode)return[3,4];t={loop:e.loop,onSourceNodeEnded:function(){u._onEnded()},mediaElement:e.audioElement},o.label=1;case 1:return o.trys.push([1,3,,4]),[4,this._playerAudio.createMediaElementSourceNode(t,e)];case 2:return o.sent(),[3,4];case 3:throw n=o.sent(),new i(n);case 4:return e.sourceNode instanceof MediaElementAudioSourceNode?[4,this._playerAudio.connectSound(e)]:[3,6];case 5:o.sent(),void 0===e.playTimeOffset||isNaN(e.playTimeOffset)||(e.audioElement.currentTime=e.playTimeOffset);try{e.sourceNode.mediaElement.play()}catch(e){throw new i(e)}o.label=6;case 6:return[2]}}))}))},e.prototype._triggerSoundCallbacks=function(e){var t=this;return null===e.onResumed||e.firstTimePlayed||e.onResumed(e.playTimeOffset),null!==e.onStarted&&e.firstTimePlayed&&e.onStarted(e.playTimeOffset),null!==e.onPlaying?this._playingProgressRequestId=window.requestAnimationFrame((function(o){t._progressTrigger(e,o)})):this._playingProgressRequestId=null,e},e.prototype._onEnded=function(){var t=this._getSoundFromQueue({whichSound:e.CURRENT_SOUND});if(null!==t&&t.state===n.SOUND_STATE_PLAYING){var o=this._getSoundFromQueue({whichSound:e.PLAY_SOUND_NEXT,updateIndex:!0});if(null!==t.onEnded){var i=!1;null!==o&&this._options.playNextOnEnded&&(i=!0),this._options.loopQueue&&(i=!0),i||this._playerAudio.freezeAudioContext(),t.onEnded(i)}t.firstTimePlayed=!0,t.playTimeOffset=0,this._stop(t,n.SOUND_STATE_STOPPED),null!==o?this._options.playNextOnEnded&&this.play({whichSound:e.PLAY_SOUND_NEXT}):(this._currentIndex=0,this._options.loopQueue&&this.play())}},e.prototype._getSoundFromQueue=function(t){var o,n=void 0===t?{}:t,i=n.whichSound,u=n.updateIndex,r=void 0!==u&&u,s=null,a=null;if(0===this._queue.length)return s;if(void 0===i)a=0,null!==this._currentIndex&&(a=this._currentIndex),s=this._queue[a];else switch(i){case e.CURRENT_SOUND:null!==this._currentIndex&&(s=this._queue[this._currentIndex]);break;case e.PLAY_SOUND_NEXT:void 0!==this._queue[this._currentIndex+1]?(a=this._currentIndex+1,s=this._queue[a]):this._options.loopQueue&&(a=0,s=this._queue[a]);break;case e.PLAY_SOUND_PREVIOUS:void 0!==this._queue[this._currentIndex-1]?(a=this._currentIndex-1,s=this._queue[a]):this._options.loopQueue&&(a=this._queue.length-1,s=this._queue[a]);break;case e.PLAY_SOUND_FIRST:this._queue.length>0&&(a=0,s=this._queue[a]);break;case e.PLAY_SOUND_LAST:this._queue.length>0&&(a=this._queue.length-1,s=this._queue[a]);break;default:s=(o=this._findSoundById({soundId:i}))[0],a=o[1]}return null!==a&&r&&(this._currentIndex=a),s},e.prototype._findSoundById=function(e){var t=e.soundId,o=null,n=0;return this._queue.some((function(e,i){if(e.id===t)return o=e,n=i,!0})),[o,n]},e.prototype._findBestSource=function(e){var t=this,o={url:null,codec:null};return(Array.isArray(e)?e:[e]).forEach((function(e){var n="";""!==t._options.soundsBaseUrl&&(n=t._options.soundsBaseUrl),n+=e.url;var i=!0;null!==e.codec&&(i=t._checkCodecSupport(e.codec)),i&&(null!==o.url&&e.isPreferred,o.url=n,o.codec=e.codec)})),o},e.prototype._checkCodecSupport=function(e){var t,o="";switch(e){case"ogg":case"oga":t=['audio/ogg; codecs="vorbis"'];break;case"mp3":t=['audio/mpeg; codecs="mp3"'];break;case"opus":t=['audio/ogg; codecs="opus"','audio/webm; codecs="opus"'];break;case"wav":t=['audio/wav; codecs="1"'];break;case"m4a":t=["audio/m4a;","audio/x-m4a;"];break;case"m4p":t=["audio/m4p;","audio/x-m4p;"];break;case"caf":t=["audio/x-caf;"];break;case"aac":t=["audio/aac;"];break;case"weba":case"webm":t=['audio/webm; codecs="vorbis"'];break;case"flac":t=["audio/flac;","audio/x-flac;"];break;default:o="unrecognised codec"}if(o)throw new i(o);return this._checkMimeTypesSupport(t)},e.prototype._checkMimeTypesSupport=function(e){var t=new Audio,o=!1;return e.forEach((function(e){t.canPlayType(e).replace(/^no$/,"")&&(o=!0)})),o},e.prototype.pause=function(){var t=this._getSoundFromQueue({whichSound:e.CURRENT_SOUND});if(null!==t&&t.state!==n.SOUND_STATE_PAUSED){var o=t.getCurrentTime();t.playTimeOffset+=o-t.startTime,null!==t.onPaused&&t.onPaused(t.playTimeOffset),this._stop(t,n.SOUND_STATE_PAUSED)}},e.prototype.stop=function(){var t=this._getSoundFromQueue({whichSound:e.CURRENT_SOUND});if(null!==t&&t.state!==n.SOUND_STATE_STOPPED){this._playerAudio.freezeAudioContext();var o=t.getCurrentTime();t.playTimeOffset+=o-t.startTime,null!==t.onStopped&&t.onStopped(t.playTimeOffset),this._stop(t,n.SOUND_STATE_STOPPED)}},e.prototype._stop=function(e,t){t===n.SOUND_STATE_STOPPED&&(e.playTimeOffset=0,e.firstTimePlayed=!0),null!==e.sourceNode&&(e.sourceNode instanceof AudioBufferSourceNode?e.sourceNode.stop(0):e.sourceNode instanceof MediaElementAudioSourceNode&&e.sourceNode.mediaElement.pause(),this._playerAudio.disconnectSound(e),e.state=t,null!==this._playingProgressRequestId&&(cancelAnimationFrame(this._playingProgressRequestId),this._playingProgressPreviousTimestamp=0))},e.prototype.next=function(){this.play({whichSound:e.PLAY_SOUND_NEXT})},e.prototype.previous=function(){this.play({whichSound:e.PLAY_SOUND_PREVIOUS})},e.prototype.first=function(){this.play({whichSound:e.PLAY_SOUND_FIRST})},e.prototype.last=function(){this.play({whichSound:e.PLAY_SOUND_LAST})},e.prototype._playingProgress=function(e){var t=e.getCurrentTime();e.playTime=t-e.startTime+e.playTimeOffset;var o=e.getDuration(),n=e.playTime/o*100;e.playedTimePercentage=n,e.onPlaying(n,o,e.playTime)},e.prototype.setVisibilityAutoMute=function(e){this._options.visibilityAutoMute=e,e?document.addEventListener("visibilitychange",this._handleVisibilityChange.bind(this),!1):document.removeEventListener("visibilitychange",this._handleVisibilityChange.bind(this),!1)},e.prototype.getVisibilityAutoMute=function(){return this._options.visibilityAutoMute},e.prototype._handleVisibilityChange=function(){var e;void 0!==document.hidden?e="hidden":void 0!==document.msHidden?e="msHidden":void 0!==document.webkitHidden&&(e="webkitHidden"),document[e]?this.mute():this.unMute()},e.prototype.disconnect=function(){return t(this,void 0,void 0,(function(){return o(this,(function(e){switch(e.label){case 0:return[4,this._playerAudio.shutDown(this._queue)];case 1:return e.sent(),[2]}}))}))},e.prototype.getAudioContext=function(){return t(this,void 0,void 0,(function(){return o(this,(function(e){switch(e.label){case 0:return[4,this._playerAudio.getAudioContext()];case 1:return[2,e.sent()]}}))}))},e.WHERE_IN_QUEUE_AT_END="append",e.WHERE_IN_QUEUE_AT_START="prepend",e.PLAY_SOUND_NEXT="next",e.PLAY_SOUND_PREVIOUS="previous",e.PLAY_SOUND_FIRST="first",e.PLAY_SOUND_LAST="last",e.CURRENT_SOUND="current",e.PLAYER_MODE_AUDIO="player_mode_audio",e.PLAYER_MODE_AJAX="player_mode_ajax",e.PLAYER_MODE_FETCH="player_mode_fetch",e}();export{s as PlayerCore,n as PlayerSound}; | ||
var e=function(t,o){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])},e(t,o)};function t(e,t,o,n){return new(o||(o=Promise))((function(i,u){function r(e){try{a(n.next(e))}catch(e){u(e)}}function s(e){try{a(n.throw(e))}catch(e){u(e)}}function a(e){var t;e.done?i(e.value):(t=e.value,t instanceof o?t:new o((function(e){e(t)}))).then(r,s)}a((n=n.apply(e,t||[])).next())}))}function o(e,t){var o,n,i,u,r={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return u={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function s(s){return function(a){return function(s){if(o)throw new TypeError("Generator is already executing.");for(;u&&(u=0,s[0]&&(r=0)),r;)try{if(o=1,n&&(i=2&s[0]?n.return:s[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,s[1])).done)return i;switch(n=0,i&&(s=[2&s[0],i.value]),s[0]){case 0:case 1:i=s;break;case 4:return r.label++,{value:s[1],done:!1};case 5:r.label++,n=s[1],s=[0];continue;case 7:s=r.ops.pop(),r.trys.pop();continue;default:if(!(i=r.trys,(i=i.length>0&&i[i.length-1])||6!==s[0]&&2!==s[0])){r=0;continue}if(3===s[0]&&(!i||s[1]>i[0]&&s[1]<i[3])){r.label=s[1];break}if(6===s[0]&&r.label<i[1]){r.label=i[1],i=s;break}if(i&&r.label<i[2]){r.label=i[2],r.ops.push(s);break}i[2]&&r.ops.pop(),r.trys.pop();continue}s=t.call(e,r)}catch(e){s=[6,e],n=0}finally{o=i=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,a])}}}var n=function(){function e(e){this.url=null,this.codec=null,this.sourceNode=null,this.gainNode=null,this.isReadyToPLay=!1,this.isBuffered=!1,this.isBuffering=!1,this.audioElement=null,this.audioBuffer=null,this.arrayBuffer=null,this.audioBufferDate=null,this.playTimeOffset=0,this.startTime=0,this.playTime=0,this.playedTimePercentage=0,this.state="sound_state_stopped",this.loadingProgress=0,this.duration=null,this.firstTimePlayed=!0,Array.isArray(e.source)?this.source=e.source:this.source=[e.source],void 0!==e.id?this.id=e.id:this.id=this._generateSoundId(),this.loop=e.loop||!1,this.duration=e.duration||null,"function"==typeof e.onLoading?this.onLoading=e.onLoading:this.onLoading=null,"function"==typeof e.onPlaying?this.onPlaying=e.onPlaying:this.onPlaying=null,"function"==typeof e.onStarted?this.onStarted=e.onStarted:this.onStarted=null,"function"==typeof e.onEnded?this.onEnded=e.onEnded:this.onEnded=null,"function"==typeof e.onStopped?this.onStopped=e.onStopped:this.onStopped=null,"function"==typeof e.onPaused?this.onPaused=e.onPaused:this.onPaused=null,"function"==typeof e.onResumed?this.onResumed=e.onResumed:this.onResumed=null,e.arrayBuffer instanceof ArrayBuffer&&(this.arrayBuffer=e.arrayBuffer),e.audioBuffer instanceof AudioBuffer&&(this.audioBuffer=e.audioBuffer,this.isBuffering=!1,this.isBuffered=!0,this.audioBufferDate=new Date,this.duration=this.getDuration())}return e.prototype.getCurrentTime=function(){var e;return null!==this.sourceNode&&(this.sourceNode instanceof AudioBufferSourceNode?e=this.sourceNode.context.currentTime:this.sourceNode instanceof MediaElementAudioSourceNode&&(e=this.audioElement.currentTime)),e},e.prototype.getDuration=function(){var e;return null!==this.sourceNode&&(this.sourceNode instanceof AudioBufferSourceNode?e=this.sourceNode.buffer.duration:this.sourceNode instanceof MediaElementAudioSourceNode&&(e=this.audioElement.duration)),e},e.prototype._generateSoundId=function(){return Date.now().toString(36)+Math.random().toString(36).substring(2)},e.SOUND_STATE_STOPPED="sound_state_stopped",e.SOUND_STATE_PAUSED="sound_state_paused",e.SOUND_STATE_PLAYING="sound_state_playing",e}(),i=function(t){function o(e,n){var i=t.call(this,e)||this;return i.code=n||null,Object.setPrototypeOf(i,o.prototype),i}return function(t,o){if("function"!=typeof o&&null!==o)throw new TypeError("Class extends value "+String(o)+" is not a constructor or null");function n(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}(o,t),o}(Error),u=function(){function e(e){this._audioContext=null,this._volume=null,this._audioNodes={gainNode:null},this._options=e,this._initialize()}return e.prototype._initialize=function(){this._options.createAudioContextOnFirstUserInteraction&&this._addAutoCreateAudioContextOnFirstUserInteractionEventListeners()},e.prototype.decodeAudio=function(e){return t(this,void 0,void 0,(function(){var t;return o(this,(function(o){switch(o.label){case 0:return[4,this.getAudioContext()];case 1:return[4,o.sent().decodeAudioData(e)];case 2:return t=o.sent(),[2,Promise.resolve(t)]}}))}))},e.prototype._createAudioContext=function(){var e=this;return new Promise((function(t,o){e._audioContext instanceof AudioContext&&t();var n=window.AudioContext||window.webkitAudioContext;try{null!==e._options.audioContext?e._audioContext=e._options.audioContext:e._audioContext=new n,t()}catch(e){o(e)}}))},e.prototype._addAutoCreateAudioContextOnFirstUserInteractionEventListeners=function(){this._options.createAudioContextOnFirstUserInteraction&&(document.addEventListener("touchstart",this.getAudioContext.bind(this)),document.addEventListener("touchend",this.getAudioContext.bind(this)),document.addEventListener("mousedown",this.getAudioContext.bind(this)))},e.prototype._removeAutoCreateAudioContextOnFirstUserInteractionEventListeners=function(){this._options.createAudioContextOnFirstUserInteraction&&(document.removeEventListener("touchstart",this.getAudioContext.bind(this)),document.removeEventListener("touchend",this.getAudioContext.bind(this)),document.removeEventListener("mousedown",this.getAudioContext.bind(this)))},e.prototype.getAudioContext=function(){return t(this,void 0,void 0,(function(){return o(this,(function(e){switch(e.label){case 0:return null!==this._audioContext?[3,2]:[4,this._createAudioContext()];case 1:return e.sent(),[3,4];case 2:return"suspended"!==this._audioContext.state?[3,4]:[4,this._unfreezeAudioContext()];case 3:e.sent(),e.label=4;case 4:return[2,this._audioContext]}}))}))},e.prototype._unfreezeAudioContext=function(){return void 0===this._audioContext.suspend?Promise.resolve():this._audioContext.resume()},e.prototype.freezeAudioContext=function(){return void 0===this._audioContext.suspend?Promise.resolve():this._audioContext.suspend()},e.prototype.detectAudioContextSupport=function(){var e=!1;return(void 0!==window.webkitAudioContext||"undefined"!=typeof AudioContext)&&(e=!0),e},e.prototype.detectAudioElementSupport=function(){return!!document.createElement("audio").canPlayType},e.prototype.shutDown=function(e){return t(this,void 0,void 0,(function(){return o(this,(function(t){switch(t.label){case 0:return this._removeAutoCreateAudioContextOnFirstUserInteractionEventListeners(),e.forEach((function(e){e.sourceNode instanceof MediaElementAudioSourceNode?void 0!==e.sourceNode.mediaElement&&e.sourceNode.mediaElement.remove():(e.sourceNode,AudioBufferSourceNode)})),this._disconnectPlayerGainNode(),[4,this._destroyAudioContext()];case 1:return t.sent(),[2]}}))}))},e.prototype._destroyAudioContext=function(){return t(this,void 0,void 0,(function(){return o(this,(function(e){switch(e.label){case 0:return null===this._audioContext?[3,2]:[4,this._audioContext.close()];case 1:e.sent(),this._audioContext=null,e.label=2;case 2:return[2]}}))}))},e.prototype.createAudioBufferSourceNode=function(e,n){return t(this,void 0,void 0,(function(){var t,i;return o(this,(function(o){switch(o.label){case 0:return[4,this.getAudioContext()];case 1:return t=o.sent(),i=t.createBufferSource(),n.sourceNode=i,i.loop=e.loop,n.gainNode=i.context.createGain(),n.gainNode.gain.value=1,i.connect(n.gainNode),i.onended=function(t){e.onSourceNodeEnded(t)},[2]}}))}))},e.prototype.createMediaElementSourceNode=function(e,n){return t(this,void 0,void 0,(function(){var u,r,s=this;return o(this,(function(a){switch(a.label){case 0:return null!==n.sourceNode?[3,2]:[4,this.getAudioContext()];case 1:r=a.sent();try{u=r.createMediaElementSource(e.mediaElement)}catch(e){throw new i(e)}u.mediaElement.loop=e.loop,n.gainNode=u.context.createGain(),n.gainNode.gain.value=1,u.connect(n.gainNode),u.mediaElement.onended=function(){return t(s,void 0,void 0,(function(){return o(this,(function(t){return e.onSourceNodeEnded(),[2]}))}))},n.sourceNode=u,a.label=2;case 2:return[2]}}))}))},e.prototype._getPlayerGainNode=function(){return t(this,void 0,void 0,(function(){var e,t;return o(this,(function(o){switch(o.label){case 0:return this._audioNodes.gainNode instanceof GainNode?(e=this._audioNodes.gainNode,[3,3]):[3,1];case 1:return[4,this.getAudioContext()];case 2:t=o.sent(),(e=t.createGain()).connect(t.destination),this._audioNodes.gainNode=e,o.label=3;case 3:return this._initializeVolume(),[2,e]}}))}))},e.prototype._disconnectPlayerGainNode=function(){this._audioNodes.gainNode.disconnect(),this._audioNodes.gainNode=null},e.prototype.connectSound=function(e){return t(this,void 0,void 0,(function(){var t,n;return o(this,(function(o){switch(o.label){case 0:return[4,this._getPlayerGainNode()];case 1:return t=o.sent(),null!==(n=e.gainNode)&&n.connect(t),[2]}}))}))},e.prototype.disconnectSound=function(e){return t(this,void 0,void 0,(function(){return o(this,(function(t){if(null===e.gainNode)throw new i("can't destroy as no source node in sound");return e.sourceNode instanceof AudioBufferSourceNode&&(e.sourceNode=null),[2]}))}))},e.prototype._changePlayerGainValue=function(e){this._audioNodes.gainNode instanceof GainNode&&(this._audioNodes.gainNode.gain.value=e)},e.prototype._roundGainTwoDecimals=function(e){return Math.round(100*(e+Number.EPSILON))/100},e.prototype.setVolume=function(e,t){void 0===t&&(t=!0),this._options.persistVolume&&t&&localStorage.setItem("WebAudioAPIPlayerVolume",e.toString());var o=e/100;this._audioNodes.gainNode instanceof GainNode&&(this._roundGainTwoDecimals(this._audioNodes.gainNode.gain.value)!==this._volume&&this._changePlayerGainValue(o));this._volume=e},e.prototype.getVolume=function(){var e;if(null!==this._volume)e=this._volume;else{if(this._options.persistVolume){var t=parseInt(localStorage.getItem("WebAudioAPIPlayerVolume"));isNaN(t)||(e=t)}void 0===e&&(e=this._options.volume)}return e},e.prototype._initializeVolume=function(){if(this._options.persistVolume){var e=parseInt(localStorage.getItem("WebAudioAPIPlayerVolume"));isNaN(e)||this.setVolume(e,!1)}null===this._volume&&this.setVolume(this._options.volume,!1)},e}(),r=function(){function e(){}return e.prototype.getArrayBuffer=function(e){return new Promise((function(t,o){var n=new XMLHttpRequest;n.open("GET",e.url,!0),n.responseType="arraybuffer",n.onload=function(){200===n.status?t(n.response):o(new i(n.statusText,n.status))},n.onprogress=function(t){var o=100/(t.total/t.loaded);e.loadingProgress=o,null!==e.onLoading&&e.onLoading(o,t.total,t.loaded)},n.onerror=function(){o(new i("xhr network error"))},n.send()}))},e}(),s=function(){function e(e){var t=this;void 0===e&&(e={}),this._playingProgressRequestId=null,this._playingProgressPreviousTimestamp=0,this._progressTrigger=function(e,o){o-t._playingProgressPreviousTimestamp>=t._options.playingProgressIntervalTime&&(t._playingProgress(e),t._playingProgressPreviousTimestamp=o),t._playingProgressRequestId=window.requestAnimationFrame((function(o){t._progressTrigger(e,o)}))};var o={volume:80,loopQueue:!1,loopSong:!1,soundsBaseUrl:"",playingProgressIntervalTime:200,playNextOnEnded:!0,stopOnReset:!0,visibilityAutoMute:!1,createAudioContextOnFirstUserInteraction:!0,persistVolume:!0,loadPlayerMode:"player_mode_audio",audioContext:null},n=Object.assign({},o,e);this._queue=[],this._currentIndex=null,this._options=n,this._initialize()}return e.prototype._initialize=function(){var t=this._audioOptions();switch(this._playerAudio=new u(t),this._options.loadPlayerMode){case e.PLAYER_MODE_AUDIO:if(!this._playerAudio.detectAudioContextSupport())throw new i("audio context is not supported by this device");if(!this._playerAudio.detectAudioElementSupport())throw new i("audio element is not supported by this device");break;case e.PLAYER_MODE_AJAX:if(!this._playerAudio.detectAudioContextSupport())throw new i("audio context is not supported by this device")}},e.prototype._audioOptions=function(){return{audioContext:this._options.audioContext,createAudioContextOnFirstUserInteraction:this._options.createAudioContextOnFirstUserInteraction,volume:this._options.volume,persistVolume:this._options.persistVolume}},e.prototype.addSoundToQueue=function(t){var o=t.soundAttributes,i=t.whereInQueue,u=void 0===i?"append":i,r=new n(o);switch(u){case e.WHERE_IN_QUEUE_AT_END:this._appendSoundToQueue(r);break;case e.WHERE_IN_QUEUE_AT_START:this._prependSoundToQueue(r)}return r},e.prototype._appendSoundToQueue=function(e){this._queue.push(e)},e.prototype._prependSoundToQueue=function(e){this._queue.unshift(e)},e.prototype.resetQueue=function(){this._options.stopOnReset&&this.stop(),this._queue=[]},e.prototype.reset=function(){this.resetQueue()},e.prototype.getQueue=function(){return this._queue},e.prototype.setVolume=function(e){this._playerAudio.setVolume(e)},e.prototype.getVolume=function(){return this._playerAudio.getVolume()},e.prototype.setLoopQueue=function(e){this._options.loopQueue=e},e.prototype.getLoopQueue=function(){return this._options.loopQueue},e.prototype.mute=function(){var e=this.getVolume();this._postMuteVolume=e,this._playerAudio.setVolume(0,!1)},e.prototype.unMute=function(){this._playerAudio.setVolume(this._postMuteVolume,!1),this._postMuteVolume=null},e.prototype.isMuted=function(){return null===this._postMuteVolume},e.prototype.setPosition=function(t){var o=this,n=this._getSoundFromQueue({whichSound:e.CURRENT_SOUND});if(null!==n)if(null===n.duration||isNaN(n.duration))this._loadSound(n).then((function(e){var n=e.duration/100*t;o.setPositionInSeconds(n)})).catch((function(e){throw e}));else{var i=n.duration/100*t;this.setPositionInSeconds(i)}},e.prototype.setPositionInSeconds=function(t){var o=this._getSoundFromQueue({whichSound:e.CURRENT_SOUND});if(null===o)throw new i("position change called, but no current sound found");o.state===n.SOUND_STATE_PLAYING?this.play({whichSound:o.id,playTimeOffset:t}):o.playTimeOffset=t},e.prototype._loadSound=function(t){var o,n;switch(this._options.loadPlayerMode){case e.PLAYER_MODE_AUDIO:o=this._loadSoundUsingAudioElement(t);break;case e.PLAYER_MODE_AJAX:o=this._loadSoundUsingRequest(t);break;case e.PLAYER_MODE_FETCH:n=new i(e.PLAYER_MODE_FETCH+" is not implemented yet",1),o=Promise.reject(n)}return o},e.prototype._loadSoundUsingAudioElement=function(e){var t=this;return new Promise((function(o,n){var u=t._findBestSource(e.source),r=u.url,s=u.codec,a=void 0===s?null:s;if(e.url=r,e.codec=a,e.arrayBuffer=null,null!==e.url){var d=new Audio;d.crossOrigin="anonymous",d.src=e.url,d.controls=!1,d.autoplay=!1,d.id="web_audio_api_player_sound_"+e.id.toString(),document.body.appendChild(d),e.audioElement=d,e.isReadyToPLay=!0,t._initializeAudioElementListeners(e);var c=function(){e.audioElement.removeEventListener("canplaythrough",c),isNaN(d.duration)||(e.duration=d.duration),o(e)};e.audioElement.addEventListener("canplaythrough",c);var l=function(){e.audioElement.removeEventListener("error",l);var t=new i("loading sound failed");n(t)};e.audioElement.addEventListener("error",l)}else{var p=new i("sound has no url",1);n(p)}}))},e.prototype._loadSoundUsingRequest=function(e){var t=this;return new Promise((function(o,n){var u=t._findBestSource(e.source),s=u.url,a=u.codec,d=void 0===a?null:a;if(e.url=s,e.codec=d,null!==e.url){var c=new r;e.isBuffering=!0,c.getArrayBuffer(e).then((function(i){e.arrayBuffer=i,t._decodeSound({sound:e}).then((function(e){o(e)})).catch(n)})).catch((function(e){n(e)}))}else{var l=new i("sound has no url",1);n(l)}}))},e.prototype._initializeAudioElementListeners=function(e){e.audioElement.addEventListener("progress",(function(){e.loadingProgress=e.audioElement.duration})),e.audioElement.addEventListener("timeupdate",(function(){e.duration=e.audioElement.duration}))},e.prototype._decodeSound=function(e){var t=e.sound;return this._playerAudio.decodeAudio(t.arrayBuffer).then((function(e){return t.audioBuffer=e,t.isBuffering=!1,t.isBuffered=!0,t.audioBufferDate=new Date,t.duration=e.duration,t.isReadyToPLay=!0,t})).catch((function(e){throw e}))},e.prototype._cloneAudioBuffer=function(e){for(var t=new AudioBuffer({length:e.length,numberOfChannels:e.numberOfChannels,sampleRate:e.sampleRate}),o=0;o<t.numberOfChannels;++o){var n=e.getChannelData(o);t.copyToChannel(n,o)}return t},e.prototype.play=function(t){var o=this,i=void 0===t?{}:t,u=i.whichSound,r=i.playTimeOffset;return new Promise((function(t,i){var s=o._getSoundFromQueue({whichSound:e.CURRENT_SOUND}),a=o._getSoundFromQueue({whichSound:u,updateIndex:!0});if(null===a)throw new Error("no more sounds in array");null===s||s.state!==n.SOUND_STATE_PLAYING&&s.state!==n.SOUND_STATE_PAUSED||s.id===a.id||o._stop(s,n.SOUND_STATE_STOPPED),null!==s&&s.state===n.SOUND_STATE_PLAYING&&s.id===a.id&&o._stop(s,n.SOUND_STATE_PAUSED),null===s||null!==s&&s.id!==a.id?a.firstTimePlayed=!0:a.firstTimePlayed=!1,void 0!==r&&(a.playTimeOffset=r),a.isReadyToPLay?(null!==a.audioBuffer&&(a.audioBuffer=o._cloneAudioBuffer(a.audioBuffer)),o._play(a).then(t).catch(i)):o._loadSound(a).then((function(){o._play(a).then(t).catch(i)})).catch(i)}))},e.prototype._play=function(e){return t(this,void 0,void 0,(function(){return o(this,(function(t){switch(t.label){case 0:return null===e.audioBuffer?[3,2]:[4,this._playAudioBuffer(e)];case 1:return t.sent(),[3,4];case 2:return[4,this._playMediaElementAudio(e)];case 3:t.sent(),t.label=4;case 4:return e.state=n.SOUND_STATE_PLAYING,e.startTime=e.getCurrentTime(),e=this._triggerSoundCallbacks(e),[2]}}))}))},e.prototype._playAudioBuffer=function(e){return t(this,void 0,void 0,(function(){var t,n,u=this;return o(this,(function(o){switch(o.label){case 0:if(null!==e.sourceNode)return[3,4];t={loop:e.loop,onSourceNodeEnded:function(){u._onEnded()}},o.label=1;case 1:return o.trys.push([1,3,,4]),[4,this._playerAudio.createAudioBufferSourceNode(t,e)];case 2:return o.sent(),[3,4];case 3:throw n=o.sent(),new i(n);case 4:return e.sourceNode instanceof AudioBufferSourceNode?(e.sourceNode.buffer=e.audioBuffer,[4,this._playerAudio.connectSound(e)]):[3,6];case 5:o.sent();try{void 0!==e.playTimeOffset?e.sourceNode.start(0,e.playTimeOffset):e.sourceNode.start()}catch(e){throw new i(e)}o.label=6;case 6:return[2]}}))}))},e.prototype._playMediaElementAudio=function(e){return t(this,void 0,void 0,(function(){var t,n,u=this;return o(this,(function(o){switch(o.label){case 0:if(null!==e.sourceNode)return[3,4];t={loop:e.loop,onSourceNodeEnded:function(){u._onEnded()},mediaElement:e.audioElement},o.label=1;case 1:return o.trys.push([1,3,,4]),[4,this._playerAudio.createMediaElementSourceNode(t,e)];case 2:return o.sent(),[3,4];case 3:throw n=o.sent(),new i(n);case 4:return e.sourceNode instanceof MediaElementAudioSourceNode?[4,this._playerAudio.connectSound(e)]:[3,6];case 5:o.sent(),void 0===e.playTimeOffset||isNaN(e.playTimeOffset)||(e.audioElement.currentTime=e.playTimeOffset);try{e.sourceNode.mediaElement.play()}catch(e){throw new i(e)}o.label=6;case 6:return[2]}}))}))},e.prototype._triggerSoundCallbacks=function(e){var t=this;return null===e.onResumed||e.firstTimePlayed||e.onResumed(e.playTimeOffset),null!==e.onStarted&&e.firstTimePlayed&&e.onStarted(e.playTimeOffset),null!==e.onPlaying?this._playingProgressRequestId=window.requestAnimationFrame((function(o){t._progressTrigger(e,o)})):this._playingProgressRequestId=null,e},e.prototype._onEnded=function(){var t=this._getSoundFromQueue({whichSound:e.CURRENT_SOUND});if(null!==t&&t.state===n.SOUND_STATE_PLAYING){var o=this._getSoundFromQueue({whichSound:e.PLAY_SOUND_NEXT,updateIndex:!0});if(null!==t.onEnded){var i=!1;null!==o&&this._options.playNextOnEnded&&(i=!0),this._options.loopQueue&&(i=!0),i||this._playerAudio.freezeAudioContext(),t.onEnded(i)}t.firstTimePlayed=!0,t.playTimeOffset=0,this._stop(t,n.SOUND_STATE_STOPPED),null!==o?this._options.playNextOnEnded&&this.play({whichSound:e.PLAY_SOUND_NEXT}):(this._currentIndex=0,this._options.loopQueue&&this.play())}},e.prototype._getSoundFromQueue=function(t){var o,n=void 0===t?{}:t,i=n.whichSound,u=n.updateIndex,r=void 0!==u&&u,s=null,a=null;if(0===this._queue.length)return s;if(void 0===i)a=0,null!==this._currentIndex&&(a=this._currentIndex),s=this._queue[a];else switch(i){case e.CURRENT_SOUND:null!==this._currentIndex&&(s=this._queue[this._currentIndex]);break;case e.PLAY_SOUND_NEXT:void 0!==this._queue[this._currentIndex+1]?(a=this._currentIndex+1,s=this._queue[a]):this._options.loopQueue&&(a=0,s=this._queue[a]);break;case e.PLAY_SOUND_PREVIOUS:void 0!==this._queue[this._currentIndex-1]?(a=this._currentIndex-1,s=this._queue[a]):this._options.loopQueue&&(a=this._queue.length-1,s=this._queue[a]);break;case e.PLAY_SOUND_FIRST:this._queue.length>0&&(a=0,s=this._queue[a]);break;case e.PLAY_SOUND_LAST:this._queue.length>0&&(a=this._queue.length-1,s=this._queue[a]);break;default:s=(o=this._findSoundById({soundId:i}))[0],a=o[1]}return null!==a&&r&&(this._currentIndex=a),s},e.prototype._findSoundById=function(e){var t=e.soundId,o=null,n=0;return this._queue.some((function(e,i){if(e.id===t)return o=e,n=i,!0})),[o,n]},e.prototype._findBestSource=function(e){var t,o={url:null,codec:null};t=Array.isArray(e)?e:[e];for(var n=0;n<t.length;){var i=t[n],u="";""!==this._options.soundsBaseUrl&&(u=this._options.soundsBaseUrl),u+=i.url;var r=!0;if(null!==i.codec&&(r=this._checkCodecSupport(i.codec)),r){if(i.isPreferred){o.url=u,o.codec=i.codec;break}o.url=u,o.codec=i.codec}n++}return o},e.prototype._checkCodecSupport=function(e){var t,o="";switch(e){case"ogg":case"oga":t=['audio/ogg; codecs="vorbis"'];break;case"mp3":t=['audio/mpeg; codecs="mp3"'];break;case"opus":t=['audio/ogg; codecs="opus"','audio/webm; codecs="opus"'];break;case"wav":t=['audio/wav; codecs="1"'];break;case"m4a":t=["audio/m4a;","audio/x-m4a;"];break;case"m4p":t=["audio/m4p;","audio/x-m4p;"];break;case"caf":t=["audio/x-caf;"];break;case"aac":t=["audio/aac;"];break;case"weba":case"webm":t=['audio/webm; codecs="vorbis"'];break;case"flac":t=["audio/flac;","audio/x-flac;"];break;default:o="unrecognised codec"}if(o)throw new i(o);return this._checkMimeTypesSupport(t)},e.prototype._checkMimeTypesSupport=function(e){var t=new Audio,o=!1;return e.forEach((function(e){t.canPlayType(e).replace(/^no$/,"")&&(o=!0)})),o},e.prototype.pause=function(){var t=this._getSoundFromQueue({whichSound:e.CURRENT_SOUND});if(null!==t&&t.state!==n.SOUND_STATE_PAUSED){var o=t.getCurrentTime();t.playTimeOffset+=o-t.startTime,null!==t.onPaused&&t.onPaused(t.playTimeOffset),this._stop(t,n.SOUND_STATE_PAUSED)}},e.prototype.stop=function(){var t=this._getSoundFromQueue({whichSound:e.CURRENT_SOUND});if(null!==t&&t.state!==n.SOUND_STATE_STOPPED){this._playerAudio.freezeAudioContext();var o=t.getCurrentTime();t.playTimeOffset+=o-t.startTime,null!==t.onStopped&&t.onStopped(t.playTimeOffset),this._stop(t,n.SOUND_STATE_STOPPED)}},e.prototype._stop=function(e,t){t===n.SOUND_STATE_STOPPED&&(e.playTimeOffset=0,e.firstTimePlayed=!0),null!==e.sourceNode&&(e.sourceNode instanceof AudioBufferSourceNode?e.sourceNode.stop(0):e.sourceNode instanceof MediaElementAudioSourceNode&&e.sourceNode.mediaElement.pause(),this._playerAudio.disconnectSound(e),e.state=t,null!==this._playingProgressRequestId&&(cancelAnimationFrame(this._playingProgressRequestId),this._playingProgressPreviousTimestamp=0))},e.prototype.next=function(){this.play({whichSound:e.PLAY_SOUND_NEXT})},e.prototype.previous=function(){this.play({whichSound:e.PLAY_SOUND_PREVIOUS})},e.prototype.first=function(){this.play({whichSound:e.PLAY_SOUND_FIRST})},e.prototype.last=function(){this.play({whichSound:e.PLAY_SOUND_LAST})},e.prototype._playingProgress=function(e){var t=e.getCurrentTime();e.playTime=t-e.startTime+e.playTimeOffset;var o=e.getDuration(),n=e.playTime/o*100;e.playedTimePercentage=n,e.onPlaying(n,o,e.playTime)},e.prototype.setVisibilityAutoMute=function(e){this._options.visibilityAutoMute=e,e?document.addEventListener("visibilitychange",this._handleVisibilityChange.bind(this),!1):document.removeEventListener("visibilitychange",this._handleVisibilityChange.bind(this),!1)},e.prototype.getVisibilityAutoMute=function(){return this._options.visibilityAutoMute},e.prototype._handleVisibilityChange=function(){var e;void 0!==document.hidden?e="hidden":void 0!==document.msHidden?e="msHidden":void 0!==document.webkitHidden&&(e="webkitHidden"),document[e]?this.mute():this.unMute()},e.prototype.disconnect=function(){return t(this,void 0,void 0,(function(){return o(this,(function(e){switch(e.label){case 0:return[4,this._playerAudio.shutDown(this._queue)];case 1:return e.sent(),[2]}}))}))},e.prototype.getAudioContext=function(){return t(this,void 0,void 0,(function(){return o(this,(function(e){switch(e.label){case 0:return[4,this._playerAudio.getAudioContext()];case 1:return[2,e.sent()]}}))}))},e.WHERE_IN_QUEUE_AT_END="append",e.WHERE_IN_QUEUE_AT_START="prepend",e.PLAY_SOUND_NEXT="next",e.PLAY_SOUND_PREVIOUS="previous",e.PLAY_SOUND_FIRST="first",e.PLAY_SOUND_LAST="last",e.CURRENT_SOUND="current",e.PLAYER_MODE_AUDIO="player_mode_audio",e.PLAYER_MODE_AJAX="player_mode_ajax",e.PLAYER_MODE_FETCH="player_mode_fetch",e}();export{s as PlayerCore,n as PlayerSound}; | ||
//# sourceMappingURL=index.min.js.map |
{ | ||
"name": "web-audio-api-player", | ||
"version": "5.0.1", | ||
"version": "5.0.2", | ||
"description": "web audio api player", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
326528
1718