0

I am trying to merge two audio files and play them.

I found this article which provide this approach:

let uris = ['file.mp3', 'file2.mp3'],
    proms = uris.map(uri => fetch(uri).then(r => r.blob()));
Promise.all(proms).then(blobs => {
    let blob = new Blob([blobs[0], blobs[1]]),
        blobUrl = URL.createObjectURL(blob),
        audio = new Audio(blobUrl);
    audio.play();
});

However. Although a merged blob is created. the audio plays only the first file.

I'll appreciate a explanation/solution;

Thank You!

  • First rule out any issues with the audio files you're working with. In the array, comment out the first file, and work just with the second. That is, work with one file at a time (effectively concatenating it with nothing) and see if you end up with a working audio file. – Mitya Jul 25 '21 at 12:21
  • @Mitya thank you for your comment. in the meantime i found out the problem was that the two files were converted to mp3 in different converters. does it make any sense to you? – yochanan sheinberger Jul 25 '21 at 17:41
  • 1
    Yes. The code presupposes that you're dealing with files of a format/encoding that the current browser supports. If that's not the case, silence is to be expected. Unfortunately, it can be annoying establishing which browsers/devices/OS's support which formats/encoding, but there is a version of the MP3 container which works everywhere, if you get the right encoding. It's the same problem with video. – Mitya Jul 25 '21 at 18:30
  • 1
    thank you. and god bless you. – yochanan sheinberger Jul 25 '21 at 18:38
  • @Mitya The website is online and the code works fine on windows and android. but i'm getting complains from apple devices users that the audio isn't playing. i think it's because the merging of the audio. do you have a solution for me? Thank You. – yochanan sheinberger Sep 02 '21 at 16:12
  • That suggests that you're encoding into a format/codec that Apple doesn't support. That is a separate question in itself, unfortunately. Inspect the format/codec used in your file, then compare to what Apple supports. – Mitya Sep 02 '21 at 18:30

0 Answers0