Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem in your code is that you're copying and appending another copy of the MP3 file onto the end of itself. That copy gets effectively ignored by the decoder - it's not raw buffer data, it's just random spurious junk in the file stream, following a perfectly complete MP3 file.</p> <p>What you need to do is first decode the audio data into a AudioBuffer, then append the audio buffers together into a new AudioBuffer. This requires a little bit of restructuring of your code.</p> <p>What you want to do is this:</p> <pre><code>var context = new webkitAudioContext(); function init() { /** * Appends two ArrayBuffers into a new one. * * @param {ArrayBuffer} buffer1 The first buffer. * @param {ArrayBuffer} buffer2 The second buffer. */ function appendBuffer(buffer1, buffer2) { var numberOfChannels = Math.min( buffer1.numberOfChannels, buffer2.numberOfChannels ); var tmp = context.createBuffer( numberOfChannels, (buffer1.length + buffer2.length), buffer1.sampleRate ); for (var i=0; i&lt;numberOfChannels; i++) { var channel = tmp.getChannelData(i); channel.set( buffer1.getChannelData(i), 0); channel.set( buffer2.getChannelData(i), buffer1.length); } return tmp; } /** * Loads a song * * @param {String} url The url of the song. */ function loadSongWebAudioAPI(url) { var request = new XMLHttpRequest(); request.open('GET', url, true); request.responseType = 'arraybuffer'; /** * Appends two ArrayBuffers into a new one. * * @param {ArrayBuffer} data The ArrayBuffer that was loaded. */ function play(data) { //decode the loaded data context.decodeAudioData(data, function(buf) { var audioSource = context.createBufferSource(); audioSource.connect(context.destination); // Concatenate the two buffers into one. audioSource.buffer = appendBuffer(buf, buf); audioSource.noteOn(0); audioSource.playbackRate.value = 1; }); }; // When the song is loaded asynchronously try to play it. request.onload = function() { play(request.response); } request.send(); } loadSongWebAudioAPI('loop.mp3'); } window.addEventListener('load',init,false); </code></pre> <p>There's a slight playback gap - that's because you have nearly 50ms of silence at the beginning of your sound sample, not due to looping issues.</p> <p>Hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload