diff --git a/dist/SFMediaStream.js b/dist/SFMediaStream.js index bfc52b6..a0e7c44 100644 --- a/dist/SFMediaStream.js +++ b/dist/SFMediaStream.js @@ -1,11 +1,9 @@ /* ScarletsFiction MediaStream Library - + HTML5 media streamer library for playing music, video, playlist, or even live streaming microphone & camera with node server https://github.com/ScarletsFiction/SFMediaStream - - Make sure you include this header on this script */ (function(global, factory){ if(typeof exports === 'object' && typeof module !== 'undefined'){ @@ -115,9 +113,15 @@ var ScarletsAudioStreamer = function(chunksDuration){ var bufferHeader = false; var mediaBuffer = false; - var audioElement = new Audio(); + var audioElement = scope.element = new Audio(); var audioNode = scope.audioContext.createMediaElementSource(audioElement); + // ToDo: we may need to try to recreate the element if error happen + // Or reducing the extra latency + audioElement.addEventListener('error', function(e){ + console.error(e.target.error); + }); + scope.connect = function(node){ if(directAudioOutput === true){ directAudioOutput = false; @@ -260,6 +264,25 @@ var ScarletsAudioStreamer = function(chunksDuration){ if(scope.debug) console.log("Total latency: "+scope.latency); } } +var BufferHeader = { + "audio/webm;codecs=opus": "GkXfo59ChoEBQveBAULygQRC84EIQoKEd2VibUKHgQRChYECGFOAZwH/////////FUmpZpkq17GDD0JATYCGQ2hyb21lV0GGQ2hyb21lFlSua7+uvdeBAXPFh7o5nyc1kHqDgQKGhkFfT1BVU2Oik09wdXNIZWFkAQIAAIC7AAAAAADhjbWERzuAAJ+BAmJkgSAfQ7Z1Af/////////ngQCjjIEAAID/A//+//7//qM=" +}; + +function getBufferHeader(type) { + var buff = BufferHeader[type]; + if(buff === void 0) return false; + + if(buff.constructor === Blob) + return buff; + + buff = atob(buff); + + var UInt = new Uint8Array(buff.length); + for (var i = 0; i < buff.length; i++) + UInt[i] = buff.charCodeAt(i); + + return BufferHeader[type] = new Blob([UInt]); +} ScarletsMedia.convert = { // Converts a MIDI pitch number to frequency. // midi = 0 ~ 127 @@ -326,7 +349,10 @@ var MediaBuffer = function(mimeType, chunksDuration, bufferHeader){ sourceBuffer.mode = 'sequence'; sourceBuffer.appendBuffer(bufferHeader); - sourceBuffer.onerror = console.error; + sourceBuffer.onerror = function(e){ + console.error("SourceBuffer error:", e); + } + sourceBuffer.onupdateend = function(){ if(removing){ removing = false; @@ -348,7 +374,9 @@ var MediaBuffer = function(mimeType, chunksDuration, bufferHeader){ // console.log(totalTime, buffer); } - scope.source.onerror = console.error; + scope.source.onerror = function(e){ + console.error("MediaSource error:", e); + } scope.append = function(arrayBuffer){ if(sourceBuffer === null) @@ -452,6 +480,11 @@ var ScarletsMediaPlayer = function(element){ element.defaultMuted = element.muted = set; } + self.stop = function(){ + self.pause(); + self.currentTime = 0; + } + var volume = 1; self.volume = function(set){ if(set === undefined) return volume; @@ -639,26 +672,26 @@ var ScarletsMediaPlayer = function(element){ shuffled:false, // lists = [{yourProperty:'', stream:['main.mp3', 'fallback.ogg', ..]}, ...] - reload:function(lists){ + reload(lists){ this.original = lists; this.shuffle(this.shuffled); internalPlaylistEvent(); }, // obj = {yourProperty:'', stream:['main.mp3', 'fallback.ogg']} - add:function(obj){ + add(obj){ this.original.push(obj); this.shuffle(this.shuffled); internalPlaylistEvent(); }, // index from 'original' property - remove:function(index){ + remove(index){ this.original.splice(index, 1); this.shuffle(this.shuffled); }, - next:function(autoplay){ + next(autoplay){ this.currentIndex++; if(this.currentIndex >= this.list.length){ if(this.loop) @@ -674,7 +707,7 @@ var ScarletsMediaPlayer = function(element){ else playlistTriggerEvent('playlistchange'); }, - previous:function(autoplay){ + previous(autoplay){ this.currentIndex--; if(this.currentIndex < 0){ if(this.loop) @@ -690,16 +723,19 @@ var ScarletsMediaPlayer = function(element){ else playlistTriggerEvent('playlistchange'); }, - play:function(index){ + play(index){ this.currentIndex = index; playlistTriggerEvent('playlistchange'); - self.prepare(this.list[index].stream, function(){ + var src = this.list[index].stream; + if(self.currentSrc === src) + self.play(); + else self.prepare(this.list[index].stream, function(){ self.play(); }); }, - shuffle:function(set){ + shuffle(set){ if(set === true){ var j, x, i; for (i = this.list.length - 1; i > 0; i--) { @@ -720,7 +756,13 @@ var ScarletsMediaPlayer = function(element){ var ScarletsMediaPresenter = function(options, latency){ var scope = this; if(!latency) latency = 1000; + + // The options are optional //var options = { + // mediaStream: new MediaStream(), // For custom media stream + // element: document.querySelector(...), // Record