Unable to play .aac extension audio file using media source extension.API

Viewed 224

I want to play a .aac audio file using MEdia source extension. I replaced the sample code with mime="audio/aac" and used .aac file. Is there any another parameter that needed to be specified.

var vidElement = document.querySelector('audio');

if (window.MediaSource) {
  var mediaSource = new MediaSource();
  vidElement.src = URL.createObjectURL(mediaSource);
  mediaSource.addEventListener('sourceopen', sourceOpen);
} else {
  console.log("The Media Source Extensions API is not supported.")
}

function sourceOpen(e) {
  URL.revokeObjectURL(vidElement.src);
  var mime = 'audio/aac';
  var mediaSource = e.target;
  var sourceBuffer = mediaSource.addSourceBuffer(mime);
  var videoUrl = 'example.aac';
  fetch(videoUrl)
    .then(function(response) {
      return response.arrayBuffer();
    })
    .then(function(arrayBuffer) {
      sourceBuffer.addEventListener('updateend', function(e) {
        if (!sourceBuffer.updating && mediaSource.readyState === 'open') {
          mediaSource.endOfStream();
        }
      });
      sourceBuffer.appendBuffer(arrayBuffer);
    });
}
1 Answers

Converting to .mp4 and setting the mimetype to audio/mp4; codecs="mp4a.40.2" worked for me. I couldn't find a way to do it without converting.

Related