Try to test it in Opera browser, in most browsers it will only work on a secure https connection. https://developer.mozilla.org/en-US/docs/Web/API/Navigator/mediaDevices
Jun 7, 2024
6
Level 2
Laravel 11 with inertia js undefined (reading 'getUserMedia')
I am getting error code in my laravel 11 inertia js project, in chrome browser, this is the error TypeError: Cannot read properties of undefined (reading 'getUserMedia') and in firefox this is the error TypeError: navigator.mediaDevices is undefined
This is my AudioRecorder.jsx file
import { useState } from "react";
import { MicrophoneIcon, StopCircleIcon } from "@heroicons/react/24/solid";
const AudioRecorder = ({ fileReady }) => {
const [mediaRecorder, setMediaRecorder] = useState(null);
const [recording, setRecording] = useState(false);
const onMicrophoneClick = async () => {
if (recording) {
setRecording(false);
if (mediaRecorder) {
mediaRecorder.stop();
setMediaRecorder(null);
}
return;
}
setRecording(true);
try {
const stream = await navigator.mediaDevices.getUserMedia({
audio: true,
});
const newMediaRecorder = new MediaRecorder(stream);
const chunks = [];
newMediaRecorder.addEventListener("dataavailable", (event) => {
chunks.push(event.data);
});
newMediaRecorder.addEventListener("stop", () => {
let audioBlob = new Blob(chunks, {
type: "audio/ogg; codecs=opus",
});
let audioFile = new File([audioBlob], "recorded_audio.ogg", {
type: "audio/ogg; codecs=opus",
});
const url = URL.createObjectURL(audioFile);
fileReady(audioFile, url);
});
newMediaRecorder.start();
setMediaRecorder(newMediaRecorder);
} catch (error) {
setRecording(false);
console.error("Error accessing microphone:", error);
}
};
return (
<button
onClick={onMicrophoneClick}
className="p-1 text-gray-400 hover:text-gray-200"
>
{recording && <StopCircleIcon className="w-6 text-red-600" />}
{!recording && <MicrophoneIcon className="w-6" />}
</button>
);
};
export default AudioRecorder;
Any help?
Please or to participate in this conversation.