0

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I used lamejs and ran into the issue that you mention above. Looks like audio-encoder uses the same underlying library.
Accepting your response since the syntax works. What’s the difference in import {abc} and import abc.
the syntax to use depends on how the thing you're importing was originally exported. there are two types of exports and imports "named" (require curly braces when importing) and "default" (no curly braces).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.