Commit edc7fb26 authored by Xiaowu Zhang's avatar Xiaowu Zhang

add blob concat when no MSE support

parent 832bc3fd
/*global window, rJS, RSVP, console, URL, Math, parseInt, document, jIO, /*global window, rJS, RSVP, console, URL, Math, parseInt, document, jIO,
Uint8Array, Audio, loopEventListener, jQuery, promiseEventListener*/ Uint8Array, Audio, loopEventListener, jQuery, promiseEventListener, Blob*/
/*jslint nomen: true, maxlen:180 */ /*jslint nomen: true, maxlen:180 */
(function(window, rJS, RSVP, loopEventListener, $, promiseEventListener) { (function(window, rJS, RSVP, loopEventListener, $, promiseEventListener) {
"use strict"; "use strict";
...@@ -40,6 +40,43 @@ ...@@ -40,6 +40,43 @@
} }
return new RSVP.Promise(itsANonResolvableTrap, canceller); return new RSVP.Promise(itsANonResolvableTrap, canceller);
}; };
function set() {
//configure a song
var gadget = this;
gadget.source.connect(gadget.filter);
gadget.filter.connect(gadget.analyser);
gadget.analyser.connect(gadget.gain);
gadget.gain.connect(audioCtx.destination);
}
function loopEvent(g) {
return new RSVP.Queue().push(function() {
if (g.rebuild) {
return g.jio_getAttachment({
_id: g.id,
_attachment: "enclosure" + g.index
});
}
}).push(function(blob) {
if (g.rebuild) {
g.index += 1;
if (g.blob) {
g.blob = new Blob([ g.blob, blob ], {
type: "audio/mpeg"
});
} else {
g.blob = new Blob([ blob ], {
type: "audio/mpeg"
});
}
g.audio.src = URL.createObjectURL(g.blob);
if (g.index < g.length) {
return loopEvent(g);
}
}
}).push(undefined, function(error) {
throw error;
});
}
function timeFormat(seconds) { function timeFormat(seconds) {
var result = "00:" + Math.round(seconds), min, sec; var result = "00:" + Math.round(seconds), min, sec;
if (seconds > 59) { if (seconds > 59) {
...@@ -59,14 +96,6 @@ ...@@ -59,14 +96,6 @@
gadget.analyser.getByteFrequencyData(array); gadget.analyser.getByteFrequencyData(array);
return array; return array;
} }
function set(url) {
//configure a song
var gadget = this;
gadget.source.connect(gadget.filter);
gadget.filter.connect(gadget.analyser);
gadget.analyser.connect(gadget.gain);
gadget.gain.connect(audioCtx.destination);
}
function promiseRequestAnimation(callback) { function promiseRequestAnimation(callback) {
var animationId, callback_promise; var animationId, callback_promise;
function canceller() { function canceller() {
...@@ -102,6 +131,9 @@ ...@@ -102,6 +131,9 @@
canvasCtx.clearRect(0, 0, cwidth, cheight); canvasCtx.clearRect(0, 0, cwidth, cheight);
step = Math.round(array.length / meterNum); step = Math.round(array.length / meterNum);
bar_context.value = that.audio.currentTime; bar_context.value = that.audio.currentTime;
if (that.audio.duration) {
bar_context.max = that.audio.duration;
}
time_context.innerHTML = timeFormat(that.audio.duration - that.audio.currentTime); time_context.innerHTML = timeFormat(that.audio.duration - that.audio.currentTime);
for (i = 0; i < meterNum; i += 1) { for (i = 0; i < meterNum; i += 1) {
value = array[i * step]; value = array[i * step];
...@@ -171,10 +203,10 @@ ...@@ -171,10 +203,10 @@
}); });
}).push(undefined, function(error) { }).push(undefined, function(error) {
if (!(error instanceof RSVP.CancellationError)) { if (!(error instanceof RSVP.CancellationError)) {
window.location = g.__element.getElementsByClassName("next")[0].href; g.rebuild = true;
return g.jio_remove({ //xxx
_id: error.id g.sourceBuffer = new Audio();
}); return;
} }
}); });
} }
...@@ -182,7 +214,7 @@ ...@@ -182,7 +214,7 @@
var g = this, command_context = g.__element.getElementsByClassName("command")[0], mute_context = g.__element.getElementsByClassName("mute")[0], bar_context = g.__element.getElementsByClassName("bar")[0], box_context = g.__element.getElementsByClassName("box")[0], filter_context = g.__element.getElementsByClassName("filter")[0], filter_type = $("select"), loop_context = g.__element.getElementsByClassName("loop")[0], loop = false, time_context = g.__element.getElementsByClassName("time")[0]; var g = this, command_context = g.__element.getElementsByClassName("command")[0], mute_context = g.__element.getElementsByClassName("mute")[0], bar_context = g.__element.getElementsByClassName("bar")[0], box_context = g.__element.getElementsByClassName("box")[0], filter_context = g.__element.getElementsByClassName("filter")[0], filter_type = $("select"), loop_context = g.__element.getElementsByClassName("loop")[0], loop = false, time_context = g.__element.getElementsByClassName("time")[0];
bar_context.value = 0; bar_context.value = 0;
return new RSVP.Queue().push(function() { return new RSVP.Queue().push(function() {
set.call(g, g.url); set.call(g);
return RSVP.all([ g.plEnablePage(), g.plGive("loop"), g.plGive("mute") ]); return RSVP.all([ g.plEnablePage(), g.plGive("loop"), g.plGive("mute") ]);
}).push(function(list) { }).push(function(list) {
if (list[1]) { if (list[1]) {
...@@ -198,6 +230,15 @@ ...@@ -198,6 +230,15 @@
time_context.style.left = bar_context.style.left; time_context.style.left = bar_context.style.left;
$(time_context).offset().top = $(bar_context).offset().top + 3; $(time_context).offset().top = $(bar_context).offset().top + 3;
time_context.innerHTML = timeFormat(g.audio.duration); time_context.innerHTML = timeFormat(g.audio.duration);
if (g.rebuild) {
return loopEvent(g);
}
}).push(function() {
if (g.blob) {
g.audio.src = URL.createObjectURL(g.blob);
g.audio.load();
g.audio.play();
}
return RSVP.any([ play.call(g), loopEventListener(g.sourceBuffer, "updateend", false, function() { return RSVP.any([ play.call(g), loopEventListener(g.sourceBuffer, "updateend", false, function() {
if (!g.fin) { if (!g.fin) {
return; return;
......
/*global window, rJS, RSVP, console, URL, Math, parseInt, document, jIO, /*global window, rJS, RSVP, console, URL, Math, parseInt, document, jIO,
Uint8Array, Audio, loopEventListener, jQuery, promiseEventListener*/ Uint8Array, Audio, loopEventListener, jQuery, promiseEventListener, Blob*/
/*jslint nomen: true, maxlen:180 */ /*jslint nomen: true, maxlen:180 */
(function (window, rJS, RSVP, loopEventListener, $, promiseEventListener) { (function (window, rJS, RSVP, loopEventListener, $, promiseEventListener) {
...@@ -54,7 +54,41 @@ ...@@ -54,7 +54,41 @@
}; };
function set() { //configure a song
var gadget = this;
gadget.source.connect(gadget.filter);
gadget.filter.connect(gadget.analyser);
gadget.analyser.connect(gadget.gain);
gadget.gain.connect(audioCtx.destination);
}
function loopEvent(g) {
return new RSVP.Queue()
.push(function () {
if (g.rebuild) {
return g.jio_getAttachment({"_id" : g.id,
"_attachment" : "enclosure" + g.index });
}
})
.push(function (blob) {
if (g.rebuild) {
g.index += 1;
if (g.blob) {
g.blob = new Blob([g.blob, blob], {type: "audio/mpeg"});
} else {
g.blob = new Blob([blob], {type: "audio/mpeg"});
}
g.audio.src = URL.createObjectURL(g.blob);
if (g.index < g.length) {
return loopEvent(g);
}
}
})
.push(undefined, function (error) {
throw error;
});
}
function timeFormat(seconds) { function timeFormat(seconds) {
var result = '00:' + Math.round(seconds), var result = '00:' + Math.round(seconds),
min, min,
...@@ -80,14 +114,6 @@ ...@@ -80,14 +114,6 @@
gadget.analyser.getByteFrequencyData(array); gadget.analyser.getByteFrequencyData(array);
return array; return array;
} }
function set(url) { //configure a song
var gadget = this;
gadget.source.connect(gadget.filter);
gadget.filter.connect(gadget.analyser);
gadget.analyser.connect(gadget.gain);
gadget.gain.connect(audioCtx.destination);
}
function promiseRequestAnimation(callback) { function promiseRequestAnimation(callback) {
var animationId, var animationId,
callback_promise; callback_promise;
...@@ -141,6 +167,9 @@ ...@@ -141,6 +167,9 @@
canvasCtx.clearRect(0, 0, cwidth, cheight); canvasCtx.clearRect(0, 0, cwidth, cheight);
step = Math.round(array.length / meterNum); step = Math.round(array.length / meterNum);
bar_context.value = that.audio.currentTime; bar_context.value = that.audio.currentTime;
if (that.audio.duration) {
bar_context.max = that.audio.duration;
}
time_context.innerHTML = timeFormat(that.audio.duration - time_context.innerHTML = timeFormat(that.audio.duration -
that.audio.currentTime); that.audio.currentTime);
for (i = 0; i < meterNum; i += 1) { for (i = 0; i < meterNum; i += 1) {
...@@ -233,9 +262,13 @@ ...@@ -233,9 +262,13 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
if (!(error instanceof RSVP.CancellationError)) { if (!(error instanceof RSVP.CancellationError)) {
window.location = g.__element g.rebuild = true;
//xxx
g.sourceBuffer = new Audio();
return;
/* window.location = g.__element
.getElementsByClassName("next")[0].href; .getElementsByClassName("next")[0].href;
return g.jio_remove({"_id" : error.id}); return g.jio_remove({"_id" : error.id});*/
} }
}); });
} }
...@@ -254,7 +287,7 @@ ...@@ -254,7 +287,7 @@
bar_context.value = 0; bar_context.value = 0;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
set.call(g, g.url); set.call(g);
return RSVP.all([ return RSVP.all([
g.plEnablePage(), g.plEnablePage(),
g.plGive("loop"), g.plGive("loop"),
...@@ -275,6 +308,16 @@ ...@@ -275,6 +308,16 @@
time_context.style.left = bar_context.style.left; time_context.style.left = bar_context.style.left;
$(time_context).offset().top = $(bar_context).offset().top + 3; $(time_context).offset().top = $(bar_context).offset().top + 3;
time_context.innerHTML = timeFormat(g.audio.duration); time_context.innerHTML = timeFormat(g.audio.duration);
if (g.rebuild) {
return loopEvent(g);
}
})
.push(function () {
if (g.blob) {
g.audio.src = URL.createObjectURL(g.blob);
g.audio.load();
g.audio.play();
}
return RSVP.any([ return RSVP.any([
play.call(g), play.call(g),
loopEventListener(g.sourceBuffer, "updateend", false, function () { loopEventListener(g.sourceBuffer, "updateend", false, function () {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment