Error while using audioencoder library in an vue3 app

Viewed 23

I need to use audioEncoder library in a Vue3 app.

https://www.npmjs.com/package/audio-encoder

I installed audioEncoder using "npm install audio-encoder"

I see the library under the "node_modules"

I am able to import this as:

import { audioEncoder } from "audio-encoder";

The following line gives an error:

    const ae = new audioEncoder();
    ae.encode(audioBuffer, 128, null, function onComplete(blob) {
      console.log(blob);
    });

The error message is: audioEncoder is not a constructor

I also tried:

    audioEncoder.encode(audioBuffer, 128, null, function onComplete(blob) {
      console.log(blob);
    });

this gives the following error: TypeError: Cannot read properties of undefined (reading 'encode')

Any recommendations on what's the best way to use this library.

Thanks!

1 Answers

I used the example code from the npm page. This code will "work":

<script setup>
import audioEncoder from 'audio-encoder';

// create audioBuffer
const audioContext = new AudioContext();
const length = 44100; // one second @ 44.1KHz
const audioBuffer = audioContext.createBuffer(1, length, 44100);
const channelData = audioBuffer.getChannelData(0);

// fill some audio
for (let i = 0; i < length; i++) {
  channelData[i] = Math.sin(i * 0.03);
}

// convert as mp3 and save file using file-saver
audioEncoder(audioBuffer, 128, null, function onComplete(blob) {
  fileSaver.saveAs(blob, 'sound.mp3');
});
</script>

I say "work" in quotations because it seems it would work if it wasn't for a known issue with the library reported here

In my opinion this library seems broken and does not look to be supported by it's author anymore. I would suggest looking for a new library, preferably one that advertises itself as Vue compatible if you can.

Related