-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from coronasafe/fix-ios-audio
- Loading branch information
Showing
7 changed files
with
1,286 additions
and
1,320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -530,17 +530,26 @@ | |
resolved "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.5.tgz" | ||
integrity sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w== | ||
|
||
"@types/react-dom@^16.9.2": | ||
version "16.9.19" | ||
resolved "https://registry.npmjs.org/@types/react-dom/-/react-dom-16.9.19.tgz" | ||
integrity sha512-xC8D280Bf6p0zguJ8g62jcEOKZiUbx9sIe6O3tT/lKfR87A7A6g65q13z6D5QUMIa/6yFPkNhqjF5z/VVZEYqQ== | ||
"@types/react-dom@18.0.11": | ||
version "18.0.11" | ||
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-18.0.11.tgz#321351c1459bc9ca3d216aefc8a167beec334e33" | ||
integrity sha512-O38bPbI2CWtgw/OoQoY+BRelw7uysmXbWvw3nLWO21H1HSh+GOlqPuXshJfjmpNlKiiSDG9cc1JZAaMmVdcTlw== | ||
dependencies: | ||
"@types/react" "^16" | ||
"@types/react" "*" | ||
|
||
"@types/react@^16", "@types/react@^16.9.9": | ||
version "16.14.43" | ||
resolved "https://registry.npmjs.org/@types/react/-/react-16.14.43.tgz" | ||
integrity sha512-7zdjv7jvoLLQg1tTvpQsm+hyNUMT2mPlNV1+d0I8fbGhkJl82spopMyBlu4wb1dviZAxpGdk5eHu/muacknnfw== | ||
"@types/react@*": | ||
version "18.2.45" | ||
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.2.45.tgz#253f4fac288e7e751ab3dc542000fb687422c15c" | ||
integrity sha512-TtAxCNrlrBp8GoeEp1npd5g+d/OejJHFxS3OWmrPBMFaVQMSN0OFySozJio5BHxTuTeug00AVXVAjfDSfk+lUg== | ||
dependencies: | ||
"@types/prop-types" "*" | ||
"@types/scheduler" "*" | ||
csstype "^3.0.2" | ||
|
||
"@types/[email protected]": | ||
version "18.2.25" | ||
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.2.25.tgz#99fa44154132979e870ff409dc5b6e67f06f0199" | ||
integrity sha512-24xqse6+VByVLIr+xWaQ9muX1B4bXJKXBbjszbld/UEDslGLY53+ZucF44HCmLbMPejTzGG9XgR+3m2/Wqu1kw== | ||
dependencies: | ||
"@types/prop-types" "*" | ||
"@types/scheduler" "*" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useState, useCallback } from 'react'; | ||
import AudioRecorder from 'audio-recorder-polyfill'; | ||
|
||
type UseAudioRecorderReturn = { | ||
status: string, | ||
startRecording: () => void, | ||
stopRecording: () => void, | ||
mediaBlobUrl: string | null, | ||
}; | ||
|
||
export const useAudioRecorder = (onStop?: (url: string) => void): UseAudioRecorderReturn => { | ||
const [status, setStatus] = useState('idle'); | ||
const [mediaBlobUrl, setMediaBlobUrl] = useState<string | null>(null); | ||
const [mediaRecorder, setMediaRecorder] = useState<MediaRecorder | null>(null); | ||
const [mediaStream, setMediaStream] = useState<MediaStream | null>(null); | ||
|
||
const startRecording = useCallback(() => { | ||
if (status !== 'idle') return; | ||
|
||
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => { | ||
setMediaStream(stream); | ||
|
||
const recorder = new AudioRecorder(stream); | ||
setMediaRecorder(recorder); | ||
|
||
const audioChunks: BlobPart[] = []; | ||
recorder.addEventListener('dataavailable', (event: any) => audioChunks.push(event.data)); | ||
recorder.addEventListener('stop', () => { | ||
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' }); | ||
const audioUrl = URL.createObjectURL(audioBlob); | ||
setMediaBlobUrl(audioUrl); | ||
onStop?.(audioUrl); | ||
setStatus('stopped'); | ||
}); | ||
|
||
recorder.start(); | ||
setStatus('recording'); | ||
}).catch(error => { | ||
console.error('Error accessing media devices:', error); | ||
}); | ||
}, [status, onStop]); | ||
|
||
const stopRecording = useCallback(() => { | ||
if (mediaRecorder && mediaRecorder.state === 'recording') { | ||
mediaRecorder.stop(); | ||
mediaStream?.getTracks().forEach(track => track.stop()); | ||
setMediaStream(null); | ||
} | ||
}, [mediaRecorder, mediaStream]); | ||
|
||
return { status, startRecording, stopRecording, mediaBlobUrl }; | ||
}; |
Oops, something went wrong.
ad5e65d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ayushma-fe – ./
ayushma-fe-ohcnetwork.vercel.app
ayushma-fe-git-master-ohcnetwork.vercel.app
ayushma-fe.vercel.app
ayushma.ohc.network