voice recognition trial 1
This commit is contained in:
parent
edc7260966
commit
0d84454a17
5 changed files with 74 additions and 88 deletions
34
app/backend/AudioRecorder.ts
Normal file
34
app/backend/AudioRecorder.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import React, { useState, useRef } from 'react'
|
||||
|
||||
export const AudioRecorder= () => {
|
||||
const [isRecording, setIsRecording] = useState(false)
|
||||
const [audioURL, setAudioURL] = useState<string | null>(null)
|
||||
const mediaRecorderRef = useRef<MediaRecorder | null>(null)
|
||||
const audioChunks = useRef<Blob[]>([])
|
||||
|
||||
const startRecording = async () => {
|
||||
const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
|
||||
const mediaRecorder = new MediaRecorder(stream)
|
||||
mediaRecorderRef.current = mediaRecorder
|
||||
|
||||
mediaRecorder.ondataavailable = (event) => {
|
||||
audioChunks.current.push(event.data)
|
||||
}
|
||||
|
||||
mediaRecorder.onstop = () => {
|
||||
const audioBlob = new Blob(audioChunks.current, { type: "audio/wav" })
|
||||
const url = URL.createObjectURL(audioBlob)
|
||||
setAudioURL(url)
|
||||
audioChunks.current = []
|
||||
}
|
||||
|
||||
mediaRecorder.start()
|
||||
setIsRecording(true)
|
||||
|
||||
}
|
||||
|
||||
const stopRecording = () => {
|
||||
mediaRecorderRef.current?.stop()
|
||||
setIsRecording(false)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue