kbumsik / opus-media-recorder

MediaRecorder polyfill for Opus recording using WebAssembly
http://kbumsik.io/opus-media-recorder/
Other
308 stars 39 forks source link

Usage with Vite? #73

Closed SupertigerDev closed 3 months ago

SupertigerDev commented 6 months ago

Does anyone know how I could use this with Vite?

charcoast commented 3 months ago

I'm using it in a react + vite application and I managed to use it like this:

import OpusMediaRecorder from 'opus-media-recorder';
import OggOpusWasm from 'opus-media-recorder/OggOpusEncoder.wasm?url';
import WebMOpusWasm from 'opus-media-recorder/WebMOpusEncoder.wasm?url';
import umd from 'opus-media-recorder/encoderWorker.umd.js?url';

export const AudioRecorder = (props) => {
...

  mediaRecorder.current = new OpusMediaRecorder(
        currentStream,
        { mimeType: 'audio/ogg' },
        {
          encoderWorkerFactory: (_: any) => new Worker(umd),
          OggOpusEncoderWasmPath: OggOpusWasm,
          WebMOpusEncoderWasmPath: WebMOpusWasm
        }
      );

...
}
SupertigerDev commented 3 months ago

Thanks! I figured it out, just forgot to close this issue. I also made an easy to use library for solidjs

https://github.com/Nerimity/solid-opus-media-recorder

admfrenchdev commented 1 week ago

I'm using it in a react + vite application and I managed to use it like this:

import OpusMediaRecorder from 'opus-media-recorder';
import OggOpusWasm from 'opus-media-recorder/OggOpusEncoder.wasm?url';
import WebMOpusWasm from 'opus-media-recorder/WebMOpusEncoder.wasm?url';
import umd from 'opus-media-recorder/encoderWorker.umd.js?url';

export const AudioRecorder = (props) => {
...

  mediaRecorder.current = new OpusMediaRecorder(
        currentStream,
        { mimeType: 'audio/ogg' },
        {
          encoderWorkerFactory: (_: any) => new Worker(umd),
          OggOpusEncoderWasmPath: OggOpusWasm,
          WebMOpusEncoderWasmPath: WebMOpusWasm
        }
      );

...
}

Hi. Trying to use this package also in a React + Vite app. Followed your implementation but when the recording starts, I'm getting this error:

TypeError: AudioContext is not a constructor

Here's my vite.config

export default defineConfig({
  plugins: [react()],
  test: {
    globals: true,
    environment: "jsdom",
    include: ["**/?(*.)test.ts?(x)"],
    setupFiles: ["./setupTests.ts"],
  },
  define: {
    global: {}, // fix Uncaught ReferenceError: global is not defined in opus-media-recorder
  },
});

Any help would be appreciated. Thx.