chrisguttandin / extendable-media-recorder

An extendable drop-in replacement for the native MediaRecorder.
MIT License
258 stars 13 forks source link

How to disconnect encoder? #666

Closed YankaZabka closed 2 years ago

YankaZabka commented 2 years ago

Hi! Thanks for this wonderful project. It helped me a lot with recording audio in 'wav' format. But i have an issue:

When my recorder starts I connect the extendable-media-recorder-wav-encoder library via await register(await connect()); But when the record stops, I want to disconnect from the encoder, how to do it right? I tried to make it through await disconnect(refMessagePort.current as MessagePort) where refMessagePort.current is the value returned by the connect() previously. But it doesn't work, because the refMessagePort.current is always null, despite the fact that this code has worked before:

      const messagePort = await connect();

      refMessagePort.current = messagePort

      await register(messagePort);
chrisguttandin commented 2 years ago

Hi @YankaZabka, it all looks good to me. connect() returns a promised MessagePort which can then be passed to disconnect() again.

It looks like you are using Refs in React to store the value. I'm not really a React expert but I know that these things can be very tricky and there might be unexpected race condition when you combine this with other hooks.

Maybe I'm missing something but if connect() returns a promised MessagePort and the Ref "forgets" about it I think there is not much that can be done to fix this here. It looks more like a React issue to me.

YankaZabka commented 2 years ago

Yeah, I think you're right. Thanks for the answer!)