I am making a system that when the file has just been read opens the next file, the files are MP3 and wanted to play a sequence of songs whenever one is finished. How to do this with FileReader ()?
I tried reader.onloadend = (function() {});
but it did not work, once the file is loaded it already executes the function, I need it to run only when it finishes being read ...
Code so far:
<input type="file" id="file" multiple="multiple">
<button id="openNewSessionButton" disabled>Open New Room</button><br />
<script>
var connection = new RTCMultiConnection('stream');
connection.session = {
audio: true,
oneway: true
};
// connect to signaling gateway
connection.connect();
// open new session
$("#openNewSessionButton").click(function() {
connection.open();
});
$("#file").change(function() {
var fileA = this.files[0];
var fileB = this.files[1];
readFile3(fileA);
});
function readFile3(file){
var file1 = URL.createObjectURL(file);
var context = new AudioContext(),
buffer;
var playAudioFile = function (buffer) {
var source = context.createBufferSource();
source.buffer = buffer;
source.connect(context.destination);
source.start(0); // Play sound immediatel
var destination = context.createMediaStreamDestination();
source.connect(destination);
connection.attachStreams.push(destination.stream);
connection.dontAttachStream = true;
$("#openNewSessionButton").removeAttr('disabled');
var current2 = source.buffer.duration;
var n = Math.floor(current2);
var b = n * 1000;
var timer = setTimeout(function(e) {
readFile3(fileB); //THIS LINE ? IS CORRECT ?
}, b);
};
var loadAudioFile = (function (url) {
var request = new XMLHttpRequest();
request.open('get', file1, true);
request.responseType = 'arraybuffer';
request.onload = function () {
context.decodeAudioData(request.response, function(incomingBuffer) {
playAudioFile(incomingBuffer);
}
);
};
request.send();
}());
</script>