From c7c22e3db1c826bcfb2bc66651ec480aae0d4ae0 Mon Sep 17 00:00:00 2001 From: yo mama Date: Sat, 4 Apr 2015 01:00:59 -0700 Subject: first --- view/poc/class/playList.js | 83 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 view/poc/class/playList.js (limited to 'view/poc/class/playList.js') diff --git a/view/poc/class/playList.js b/view/poc/class/playList.js new file mode 100644 index 0000000..b161ce8 --- /dev/null +++ b/view/poc/class/playList.js @@ -0,0 +1,83 @@ +var PlayList=function(el){ + var playList= el, + files={}, + list=[], + loops=4, + status = { + playing : false, + current : 0, + currentLoop : 0 + }, + audioBuffer = null; + + window.AudioContext = window.AudioContext || window.webkitAudioContext; + var context = new AudioContext(); + + function addItem(data){ + list.push(data.url); + playList.innerHTML+='
  • '+ + data.url+ + '
  • '; + + if(!status.playing) + play(data.url); + } + + function play(data){ + audioBuffer = context.createBufferSource(); + audioBuffer.buffer=(typeof data=='string')? + ( + function(){ + status.currentLoop=0; + return files[data]; + } + )():data; + audioBuffer.connect(context.destination); + status.playing=true; + audioBuffer.start(0); + audioBuffer.onended=function(){ + status.currentLoop++; + if(status.currentLoop>=loops){ + status.playing=false; + next(); + return; + } + play(this.buffer); + } + } + + function next(){ + var count = list.length; + status.current++; + if(status.current>=count) + status.current=0; + if(!status.playing) + play(list[status.current]); + } + + function recievedAudio(data){ + if(files[data.url]){ + data.file=null; + addItem(data); + return; + } + + var audioBinary=Base64Binary.decodeArrayBuffer(data.file); + context.decodeAudioData( + audioBinary, + function(buffer) { + files[data.url]=buffer; + data.file=null; + addItem(data); + } + ); + } + + return { + add : addItem, + is : status, + on : { + add:recievedAudio + } + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2