11

I'm creating a chrome app that decrypts mp3s sent from my PBX server to my gmail account and plays them. I have completed everything except for the audio player in gmail. I have two options:

  1. Use Web Audio API (I got it working but can't figure out how to make a fully functional seek bar).
  2. Create an createObjectURL from the array and pass to either audio tag or soundmanager2.

I want to reuse code as much as possible and haven't been able to find a pre-made Web Audio API player with seekbar. So, I attempted to try option 2 and the following is as far as I went

window.AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new AudioContext();
context.decodeAudioData(arr.buffer, function (soundBuffer) {
    windowURL = window.URL || window.webkitURL;
    var audio = document.createElement("audio");
    audio.src = windowURL.createObjectURL([soundBuffer]);
    var someDiv = document.getElementById("testDiv");
    someDiv.appendChild(audio);
    audio.onload = function (e) {
        windowURL.revokeObjectURL(this.src);
    }
}, function (err) {
   console.log("couldnt decode buffer");
});

It fails with "Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided." How should I properly code this function to create an url that can used by chrome's mp3 player or soundmanager2?

5
  • Is my question difficult to understand?
    – SILENT
    Commented Jun 24, 2014 at 0:59
  • Gmail plays MP3 attachments for me already. Are you sure you need to make a browser extension for this?
    – Brad
    Commented Jun 24, 2014 at 2:56
  • @Brad Gmail plays my mp3s as well. However, the mp3s sent from my PBX is encrypted and thus won't play unless I decrypt. My app decrypts the mp3s to play it. However, Im stuck on the stage where I have the soundbuffer but unable to play it with a versatile player (play/stop/seek/volume)
    – SILENT
    Commented Jun 24, 2014 at 3:04
  • @SILENT did you solve this? I would appreciate the answer. Thanks
    – elranu
    Commented Sep 1, 2015 at 13:05
  • 1
    @elranu I reattempted Option 1 and was able to script a player with seek capabilities.
    – SILENT
    Commented Sep 13, 2015 at 15:57

2 Answers 2

7

You need to first create a Blob, and pass that as an argument for createObjectURL

....
const blob = new Blob([soundBuffer], { type: "audio/wav" });
audio.src = window.URL.createObjectURL(blob);
....

Source

2
  • 2
    Note that if you need to decode the arrayBuffer, this will detach the buffer, thus you won't be able to create the blob. Solution: get the array buffer, create the blob, decode the array buffer.
    – JohnDoe
    Commented Mar 15, 2020 at 9:47
  • Holy god Mr. JohnDoe, I've been looking for answers, thank. you!
    – Dale Ryan
    Commented Jun 9 at 12:08
2

Yes, you can do it

  1. Fetch arraybuffer and decode audio
  2. Create MediaSource with listener 'sourceopen'
  3. Use method appendBuffer to add you decoded audio to MediaSource

https://developer.mozilla.org/en-US/docs/Web/API/MediaSource

Not the answer you're looking for? Browse other questions tagged or ask your own question.