diff options
Diffstat (limited to 'view/poc/class/playList.js')
| -rw-r--r-- | view/poc/class/playList.js | 83 |
1 files changed, 83 insertions, 0 deletions
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+='<li>'+ + data.url+ + '</li>'; + + 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 |
