Working Audio Loop Example in Dart

Issue

I’m trying to use Dart to get an OGG file to loop using the HTML5 <audio> element. Does anyone have a working example for this. I’m specifically having trouble getting the audio to loop.

Solution

I was not able to have a fully controlled loop using the html5 AudioElement; sometimes the loop option was simply not working, sometimes there was a gap, sometimes patterns would overlap.

I had better chance using WebAudio using something like:

source = audioContext.createBufferSource();
source.buffer = buffer;

gainNode = audioContext.createGain();
gainNode.gain.value = 1;

source.connectNode(gainNode);
gainNode.connectNode(audioContext.destination);

// play it now in loop
source.start(audioContext.currentTime);
source.loop = true;

I was not able to load the source buffer from the html audio element which could have been a solution for the CORS issues I had. The samples were loaded using http requests.

I created a dartpad example that demonstrates looping using AudioElement native loop feature and WebAudio

https://dartpad.dartlang.org/879424bca794c63698b0

Answered By – alextk

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *