Skip to content

Commit

Permalink
feat: add meterings param to onRecordStop event
Browse files Browse the repository at this point in the history
  • Loading branch information
lodev09 committed May 16, 2024
1 parent d6d8307 commit e9e9574
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
7 changes: 6 additions & 1 deletion example/components/ThemedRecorderSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,17 @@ export const ThemedRecorderSheet = forwardRef(
scale.value = withSpring(RECORDING_INDICATOR_SCALE, SPRING_SHORT_CONFIG)
setIsRecording(true)
}}
onRecordStop={(uri) => {
onRecordStop={(uri, duration, meterings) => {
scale.value = withSpring(1, SPRING_SHORT_CONFIG)
setIsRecording(false)

recorderRef.current?.startPlayback()

// Use this uri. Yay! 🎉
console.log(uri)

console.log(duration)
console.log(meterings)
}}
onPlaybackStart={() => setIsPlaying(true)}
onPlaybackStop={() => setIsPlaying(false)}
Expand Down
58 changes: 27 additions & 31 deletions src/Recorder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,40 +195,36 @@ export const Recorder = forwardRef((props: RecorderProps, ref: Ref<RecorderRef>)
if (!isRecording) return
if (!recording.current) return

try {
await recording.current?.stopAndUnloadAsync()
await Audio.setAudioModeAsync({ allowsRecordingIOS: false })

let durationMillis: number | undefined
const uri = recording.current?.getURI()
if (uri) {
const newSound = new Audio.Sound()
newSound.setOnPlaybackStatusUpdate(handlePlaybackStatus)

await newSound.loadAsync({ uri })
await newSound.setProgressUpdateIntervalAsync(progressInterval)

// Sync position and duration ms
const currentStatus = await newSound.getStatusAsync()
if (currentStatus.isLoaded) {
durationMillis = currentStatus.durationMillis ?? duration
await newSound.setPositionAsync(durationMillis)

updatePosition(durationMillis)
setDuration(durationMillis)
}

sound.current = newSound
recordingUri.current = uri
await recording.current?.stopAndUnloadAsync()
await Audio.setAudioModeAsync({ allowsRecordingIOS: false })

let durationMillis: number | undefined
const uri = recording.current?.getURI()
if (uri) {
const newSound = new Audio.Sound()
newSound.setOnPlaybackStatusUpdate(handlePlaybackStatus)

await newSound.loadAsync({ uri })
await newSound.setProgressUpdateIntervalAsync(progressInterval)

// Sync position and duration ms
const currentStatus = await newSound.getStatusAsync()
if (currentStatus.isLoaded) {
durationMillis = currentStatus.durationMillis ?? duration
await newSound.setPositionAsync(durationMillis)

updatePosition(durationMillis)
setDuration(durationMillis)
}

onRecordStop?.(uri, durationMillis)
} catch (e) {
console.error(e)
} finally {
recording.current = undefined
setIsRecording(false)
sound.current = newSound
recordingUri.current = uri
}

recording.current = undefined
setIsRecording(false)

onRecordStop?.(uri, durationMillis, meterings)
}

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/Recorder.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export interface RecorderProps extends Omit<ViewProps, 'children'> {
* @param position - the current position it stopped.
* @param duration - the duration of the recording
*/
onRecordStop?: (uri?: string | null, duration?: number) => void
onRecordStop?: (uri?: string | null, duration?: number, meterings?: Metering[]) => void

/**
* Called when recording has been reset.
Expand Down

0 comments on commit e9e9574

Please sign in to comment.