I'm programing a react app that has a integration with the what's app api. To be able to send voice messages, I need to get a .ogg codecs=opus or .opus audio file format from my browser, I'm trying to use the library opus-coder for it.
As far I can see, I'm using the library correctly, but receiving "TypeError: recorder.current.init is not a function". Has been two days since I'm struggling to be able to send voice messages, please someone who know's better this field show what I need to do, i feel is something not that hard.
Here's the relevant part of my code:
import Recorder from "opus-recorder";
import encoderPath from "opus-recorder/dist/encoderWorker.min.js";
const recorder = useRef(null);
const startRecording = () => {
// Verifica se o navegador suporta getUserMedia
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
// Solicita permissão para acessar o microfone
navigator.mediaDevices
.getUserMedia({ audio: true })
.then(async (stream) => {
// Cria uma instância do Recorder
recorder.current = new Recorder({
encoderPath: encoderPath, // Caminho para o arquivo do worker do encoder
leaveStreamOpen: true, // Manter a stream de áudio aberta após a gravação
encoderSampleRate: 48000, // Taxa de amostragem do encoder (padrão: 48000)
encoderApplication: 2049, // Aplicação de codificação do encoder (padrão: 2049)
});
console.log(recorder.current);
// Inicia a gravação
await recorder.current.init(stream);
await recorder.current.start();
console.log("Gravação iniciada...");
})
.catch((error) => {
console.error("Erro ao acessar a mídia:", error);
});
}
};
const stopRecording = async () => {
// Verifica se existe um recorder em execução
if (recorder.current) {
// Para a gravação
await recorder.current.stop();
console.log("Gravação encerrada...");
// Obtém o áudio gravado
const audioData = await recorder.current.exportWAV();
const audioUrl = URL.createObjectURL(audioData);
// Cria um link para fazer o download do áudio
const downloadLink = document.createElement("a");
downloadLink.href = audioUrl;
downloadLink.download = "teste.opus";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
console.log(audioUrl);
recorder.current.close();
}
};