-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioRecorder.ts
More file actions
78 lines (69 loc) · 1.92 KB
/
AudioRecorder.ts
File metadata and controls
78 lines (69 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Audio } from 'expo-av'
import ExponentAV from 'expo-av/src/ExponentAV'
export default class AR {
private static recording = new Audio.Recording()
private static isOperating = false
public static startRecording = async () => {
if (AR.isOperating) return false
try {
AR.isOperating = true
const status = await AR.status
if (status.isRecording) {
await AR.prepare()
}
if (status.canRecord) {
await AR.recording.startAsync()
return true
} else {
return false
}
} catch (e: any) {
await AR.handleStopError(e)
return false
} finally {
AR.isOperating = false
}
}
public static stopRecording = async () => {
if (AR.isOperating) return null
let uri: string | null = null
try {
AR.isOperating = true
if ((await AR.status).isRecording) {
await AR.recording.stopAndUnloadAsync()
uri = AR.recording.getURI()
}
} catch (e: any) {
await AR.handleStopError(e)
} finally {
await AR.prepare()
AR.isOperating = false
}
return uri
}
private static prepare = async () => {
const status = await AR.status
if (status.canRecord) return
try {
if (status.isRecording) await AR.recording.stopAndUnloadAsync()
} catch (e: any) {
await AR.handleStopError(e)
}
AR.recording = new Audio.Recording()
await AR.recording.prepareToRecordAsync(Audio.RecordingOptionsPresets.HIGH_QUALITY)
}
private static handleStopError = async (e: any) => {
if (e.message.includes('Stop encountered an error')) {
await ExponentAV.unloadAudioRecorder()
await AR.recording._cleanupForUnloadedRecorder({ durationMillis: 0 } as any)
}
}
public static get status() {
return AR.recording.getStatusAsync()
}
static {
AR.prepare()
}
}
export { AR as AudioRecorder }