-
Notifications
You must be signed in to change notification settings - Fork 1
/
audio.min.js
1 lines (1 loc) · 6.44 KB
/
audio.min.js
1
"use strict";function AudioEngine(i,e){this.common=flowplayer.common,this.bean=flowplayer.bean,this.support=flowplayer.support,this.player=i,this.root=e,this.rootClasses=e.classList,this.api=this.common.find("audio",e)[0],this.engineName="audio",this.conf=i.conf,this.timer,this.volumeLevel,this.created=!1,this.reload=!1,this.originalSources=[],this.fallbackIndex=0}function AudioEngineWrapper(i,e){return new AudioEngine(i,e)}!function(){var i=flowplayer.support,e=$("<audio/>")[0];flowplayer.extend(i,{audio:!!e.canPlayType})}(),AudioEngine.AUDIO=$("<audio/>")[0],AudioEngine.AUDIO_TYPES={mp4:'audio/mp4; codecs="mp4a.40.2, mp4a.40.5"',mpeg:'audio/mpeg; codecs="mp3"',ogg:'audio/ogg; codecs="vorbis"',"audio/mp4":'audio/mp4; codecs="mp4a.40.2, mp4a.40.5"',"audio/mpeg":'audio/mpeg; codecs="mp3"',"audio/ogg":'audio/ogg; codecs="vorbis"'},AudioEngine.getAudioType=function(i){return/mpegurl/i.test(i)?"application/x-mpegurl":"audio/"+i},AudioEngine.canPlayAudio=function(i){return/^(audio|application)/i.test(i)||(i=AudioEngine.getAudioType(i)),!!AudioEngine.AUDIO.canPlayType(AudioEngine.AUDIO_TYPES[i]).replace("no","")},AudioEngine.prototype.pick=function(i){if(flowplayer.support.audio)for(var e=0;e<i.length;e++)if(AudioEngine.canPlayAudio(i[e].type))return i[e]},AudioEngine.prototype.createAudioTag=function(i,e,t){"undefined"==typeof e&&(e=!0),"undefined"==typeof t&&(t="none");var o=document.createElement("audio");return o.src=i.src,o.type=AudioEngine.getAudioType(i.type),o.className="fp-engine",o.autoplay=e?"autoplay":!1,o.preload=t,o.setAttribute("x-webkit-airplay","allow"),o},AudioEngine.prototype.load=function(i){var e=this.common.find(".fp-player",this.root)[0];this.conf.splash&&!this.api?(this.api=this.createAudioTag(i),this.common.prepend(e,this.api),this.created=!0):this.api?(this.api.classList.add("fp-engine"),this.common.find("source,track",this.api).forEach(this.common.removeNode),this.reload=this.api.src===i.src):(this.api=this.createAudioTag(i,!!i.autoplay||!!this.conf.autoplay,this.conf.clip.preload||"metadata",!1),this.common.prepend(e,this.api),this.created=!0),flowplayer.support.inlineVideo||this.common.css(this.api,{position:"absolute",top:"-9999em"}),this.common.prop(this.api,"loop",!(!i.loop&&!this.conf.loop)),"undefined"!=typeof this.volumeLevel&&(this.api.volume=this.volumeLevel),(this.player.video.src&&i.src!=this.player.video.src||i.index)&&this.common.attr(this.api,"autoplay","autoplay"),this.api.src=i.src,this.api.type=i.type,this.listen(this.api,this.common.find("source",this.api).concat(this.api),i),("none"!=this.conf.clip.preload&&"mpegurl"!=i.type||!flowplayer.support.zeropreload||!flowplayer.support.dataload)&&this.api.load(),(this.created||this.reload)&&this.api.load(),this.api.paused&&i.autoplay&&this.api.play()},AudioEngine.prototype.pause=function(){this.api.pause()},AudioEngine.prototype.resume=function(){this.api.play()},AudioEngine.prototype.speed=function(i){this.api.playbackRate=i},AudioEngine.prototype.seek=function(i){try{var e=this.player.paused;this.api.currentTime=i,e&&this.api.pause()}catch(t){}},AudioEngine.prototype.volume=function(i){this.volumeLevel=i,this.api&&(this.api.volume=i)},AudioEngine.prototype.unload=function(){this.common.removeNode(this.common.find("audio.fp-engine",this.root)[0]),this.timer=clearInterval(this.timer),this.api=0},AudioEngine.prototype.triggerEvent=function(i,e){this.player.trigger(i,[this.player,e])},AudioEngine.prototype.onEnded=function(i){this.triggerEvent("finish")},AudioEngine.prototype.onPaused=function(i){this.triggerEvent("pause")},AudioEngine.prototype.onBuffer=function(i){this.triggerEvent("buffer")},AudioEngine.prototype.onPlay=function(i){this.triggerEvent("resume")},AudioEngine.prototype.onProgress=function(i){var e;(this.api.currentTime>0||this.player.live)&&(e=this.api.currentTime>0?this.api.currentTime:0),this.triggerEvent("progress",e)},AudioEngine.prototype.onSeek=function(i){var e;(this.api.currentTime>0||this.player.live)&&(e=this.api.currentTime>0?this.api.currentTime:0),this.triggerEvent("seek",e)},AudioEngine.prototype.onVolumeChange=function(i){var e=this.api.volume;this.triggerEvent("volume",e)},AudioEngine.prototype.onSpeed=function(i){var e=this.api.playbackRate;this.triggerEvent("speed",e)},AudioEngine.prototype.onError=function(i){var e=(i.srcElement||i.originalTarget).error;this.triggerEvent("error",e)},AudioEngine.prototype.onReady=function(i){var e=flowplayer.extend(this.player.video,{duration:this.api.duration,width:this.api.videoWidth,height:this.api.videoHeight,url:this.api.currentSrc,src:this.api.currentSrc,seekable:!0});this.timer=this.timer||setInterval(function(){try{e.buffer=this.api.buffered.end(null)}catch(t){}e.buffer&&(~~(.5+e.buffer)<~~(.5+e.duration)&&!e.buffered?this.player.trigger("buffer",i):e.buffered||(e.buffered=!0,this.player.trigger("buffer",i).trigger("buffered",i),clearInterval(this.timer),this.timer=0))}.bind(this),250),this.triggerEvent("ready",e)},AudioEngine.prototype.listen=function(i,e,t){var o=this.root.getAttribute("data-flowplayer-instance-id");return i.listeners&&i.listeners.hasOwnProperty(o)?void(i.listeners[o]=t):((i.listeners||(i.listeners={}))[o]=t,this.player.on("error",function(i,e,t){this.fallbackIndex<this.originalSources.length&&this.rootClasses.remove("is-error")}.bind(this)),this.bean.on(e,"error",function(e){this.rootClasses.remove("is-error"),AudioEngine.canPlayAudio(e.target.getAttribute("type"))&&this.fallbackIndex>=this.originalSources.length?this.player.trigger("error",{code:4,video:extend(t,{src:i.src,url:i.src})}):(this.rootClasses.remove("is-error"),this.fallbackIndex++,this.load(this.originalSources[this.fallbackIndex]))}.bind(this)),this.api.addEventListener("ended",this.onEnded.bind(this)),this.api.addEventListener("pause",this.onPaused.bind(this)),this.api.addEventListener("play",this.onPlay.bind(this)),this.api.addEventListener("progress",this.onBuffer.bind(this)),this.api.addEventListener("timeupdate",this.onProgress.bind(this)),this.api.addEventListener("volumechange",this.onVolumeChange.bind(this)),this.api.addEventListener("ratechange",this.onSpeed.bind(this)),this.api.addEventListener("seeked",this.onSeek.bind(this)),this.api.addEventListener("loadedmetadata",this.onReady.bind(this)),this.api.addEventListener("error",this.onError.bind(this)),void this.api.addEventListener("dataunavailable",this.onError.bind(this)))};var audioEngine=AudioEngineWrapper;audioEngine.canPlay=function(i){return flowplayer.support.audio&&AudioEngine.canPlayAudio(i)},audioEngine.engineName="audio",flowplayer.engines.unshift(audioEngine);