2
I’m 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 finishes. How to do this with Filereader()?
I tried to reader.onloadend = (function() {}); but it didn’t work, as soon as the file is loaded it already performs the function, I need it to run only when it finishes being read...
Code to date:
<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>
						
How strange... I have one example on my website, and use
readAsArrayBufferwith the eventonloadand everything works well. The file is already in memory whenonloadis executed. Take a look at the functionloadIntoMemoryAndPlay, line 294 of the page (tested OK on Chrome, Firefox and Opera).– carlosrafaelgn
You really need to use one
FileReader()? Can you play the audio via another method? If so, take a look at the functionprepareStreamingAndPlayof my example, line 314 of the file.– carlosrafaelgn
Just one detail... I’ve been editing the example, and now the line numbers are 302 (
loadIntoMemoryAndPlay) and 339 (prepareStreamingAndPlay) :)– carlosrafaelgn
Hi @carlosrafaelgn, I saw your examples and found it really cool and I even changed the system I was using, but I can’t move to the next track when the first one ends, I edited the question and added the code I’m using now......
– Alan PS
@carlosrafaelgn got some result...I will post the answer...
– Alan PS
Funny, really! Looks like an unsolved bug... : P
– carlosrafaelgn