11 lines
10 KiB
JavaScript
Raw Normal View History

// ----------------------------------------------------------------------------
// Buzz, a Javascript HTML5 Audio library
// v1.2.0 - Built 2016-05-22 15:16
// Licensed under the MIT license.
// http://buzz.jaysalvat.com/
// ----------------------------------------------------------------------------
// Copyright (C) 2010-2016 Jay Salvat
// http://jaysalvat.com/
// ----------------------------------------------------------------------------
!function(a,b){"use strict";"undefined"!=typeof module&&module.exports?module.exports=b():"function"==typeof define&&define.amd?define([],b):a.buzz=b()}(this,function(){"use strict";var a=window.AudioContext||window.webkitAudioContext,b={defaults:{autoplay:!1,crossOrigin:null,duration:5e3,formats:[],loop:!1,placeholder:"--",preload:"metadata",volume:80,webAudioApi:!1,document:window.document},types:{mp3:"audio/mpeg",ogg:"audio/ogg",wav:"audio/wav",aac:"audio/aac",m4a:"audio/x-m4a"},sounds:[],el:document.createElement("audio"),getAudioContext:function(){if(void 0===this.audioCtx)try{this.audioCtx=a?new a:null}catch(b){this.audioCtx=null}return this.audioCtx},sound:function(a,c){function d(a){for(var b=[],c=a.length-1,d=0;c>=d;d++)b.push({start:a.start(d),end:a.end(d)});return b}function e(a){return a.split(".").pop()}c=c||{};var f=c.document||b.defaults.document,g=0,h=[],i={},j=b.isSupported();if(this.load=function(){return j?(this.sound.load(),this):this},this.play=function(){return j?(this.sound.play(),this):this},this.togglePlay=function(){return j?(this.sound.paused?this.sound.play():this.sound.pause(),this):this},this.pause=function(){return j?(this.sound.pause(),this):this},this.isPaused=function(){return j?this.sound.paused:null},this.stop=function(){return j?(this.setTime(0),this.sound.pause(),this):this},this.isEnded=function(){return j?this.sound.ended:null},this.loop=function(){return j?(this.sound.loop="loop",this.bind("ended.buzzloop",function(){this.currentTime=0,this.play()}),this):this},this.unloop=function(){return j?(this.sound.removeAttribute("loop"),this.unbind("ended.buzzloop"),this):this},this.mute=function(){return j?(this.sound.muted=!0,this):this},this.unmute=function(){return j?(this.sound.muted=!1,this):this},this.toggleMute=function(){return j?(this.sound.muted=!this.sound.muted,this):this},this.isMuted=function(){return j?this.sound.muted:null},this.setVolume=function(a){return j?(0>a&&(a=0),a>100&&(a=100),this.volume=a,this.sound.volume=a/100,this):this},this.getVolume=function(){return j?this.volume:this},this.increaseVolume=function(a){return this.setVolume(this.volume+(a||1))},this.decreaseVolume=function(a){return this.setVolume(this.volume-(a||1))},this.setTime=function(a){if(!j)return this;var b=!0;return this.whenReady(function(){b===!0&&(b=!1,this.sound.currentTime=a)}),this},this.getTime=function(){if(!j)return null;var a=Math.round(100*this.sound.currentTime)/100;return isNaN(a)?b.defaults.placeholder:a},this.setPercent=function(a){return j?this.setTime(b.fromPercent(a,this.sound.duration)):this},this.getPercent=function(){if(!j)return null;var a=Math.round(b.toPercent(this.sound.currentTime,this.sound.duration));return isNaN(a)?b.defaults.placeholder:a},this.setSpeed=function(a){return j?(this.sound.playbackRate=a,this):this},this.getSpeed=function(){return j?this.sound.playbackRate:null},this.getDuration=function(){if(!j)return null;var a=Math.round(100*this.sound.duration)/100;return isNaN(a)?b.defaults.placeholder:a},this.getPlayed=function(){return j?d(this.sound.played):null},this.getBuffered=function(){return j?d(this.sound.buffered):null},this.getSeekable=function(){return j?d(this.sound.seekable):null},this.getErrorCode=function(){return j&&this.sound.error?this.sound.error.code:0},this.getErrorMessage=function(){if(!j)return null;switch(this.getErrorCode()){case 1:return"MEDIA_ERR_ABORTED";case 2:return"MEDIA_ERR_NETWORK";case 3:return"MEDIA_ERR_DECODE";case 4:return"MEDIA_ERR_SRC_NOT_SUPPORTED";default:return null}},this.getStateCode=function(){return j?this.sound.readyState:null},this.getStateMessage=function(){if(!j)return null;switch(this.getStateCode()){case 0:return"HAVE_NOTHING";case 1:return"HAVE_METADATA";case 2:return"HAVE_CURRENT_DATA";case 3:return"HAVE_FUTURE_DATA";case 4:return"HAVE_ENOUGH_DATA";default:return null}},this.getNetworkStateCode=function(){return j?this.sound.networkState:null},this.getNetworkStateMessage=function(){if(!j)return null;switch(this.getNetworkStateCode()){case 0:return"NETWORK_EMPTY";case 1:return"NETWORK_IDLE";case 2:return"NETWORK