-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathRecordAudio.tsx
More file actions
205 lines (193 loc) · 5.77 KB
/
RecordAudio.tsx
File metadata and controls
205 lines (193 loc) · 5.77 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { View, Text } from 'react-native';
import React, { type ReactElement, useEffect, useRef } from 'react';
import { Audio } from 'expo-av';
import { getInfoAsync } from 'expo-file-system/legacy';
import { useKeepAwake } from 'expo-keep-awake';
import { shallowEqual } from 'react-redux';
import { useTheme } from '../../../../theme';
import { BaseButton } from '../Buttons';
import { CustomIcon } from '../../../CustomIcon';
import sharedStyles from '../../../../views/Styles';
import { ReviewButton } from './ReviewButton';
import { useMessageComposerApi } from '../../context';
import { sendFileMessage } from '../../../../lib/methods/sendFileMessage';
import { RECORDING_EXTENSION, RECORDING_MODE, RECORDING_SETTINGS } from '../../../../lib/constants/audio';
import { useAppSelector } from '../../../../lib/hooks/useAppSelector';
import log from '../../../../lib/methods/helpers/log';
import { type IUpload } from '../../../../definitions';
import { useRoomContext } from '../../../../views/RoomView/context';
import { useCanUploadFile } from '../../hooks';
import { Duration, type IDurationRef } from './Duration';
import AudioPlayer from '../../../AudioPlayer';
import { CancelButton } from './CancelButton';
import i18n from '../../../../i18n';
export const RecordAudio = (): ReactElement | null => {
const [styles, colors] = useStyle();
const recordingRef = useRef<Audio.Recording | null>(null);
const durationRef = useRef<IDurationRef>({} as IDurationRef);
const numberOfTriesRef = useRef(0);
const [status, setStatus] = React.useState<'recording' | 'reviewing'>('recording');
const { setRecordingAudio } = useMessageComposerApi();
const { rid, tmid } = useRoomContext();
const server = useAppSelector(state => state.server.server);
const user = useAppSelector(state => ({ id: state.login.user.id, token: state.login.user.token }), shallowEqual);
const permissionToUpload = useCanUploadFile(rid);
useKeepAwake();
useEffect(() => {
const record = async () => {
try {
await Audio.setAudioModeAsync(RECORDING_MODE);
recordingRef.current = new Audio.Recording();
await recordingRef.current.prepareToRecordAsync(RECORDING_SETTINGS);
recordingRef.current.setOnRecordingStatusUpdate(durationRef.current.onRecordingStatusUpdate);
await recordingRef.current.startAsync();
} catch (error: any) {
// error only occurs on iOS devices
if (error?.code === 'E_AUDIO_RECORDERNOTCREATED') {
if (numberOfTriesRef.current <= 5) {
recordingRef.current = null;
numberOfTriesRef.current += 1;
setTimeout(() => {
record();
}, 100);
} else {
console.error(error);
}
} else {
console.error(error);
}
}
};
record();
return () => {
try {
recordingRef.current?.stopAndUnloadAsync();
} catch {
// Do nothing
}
};
}, []);
const cancelRecording = async () => {
try {
await recordingRef.current?.stopAndUnloadAsync();
} catch {
// Do nothing
} finally {
setRecordingAudio(false);
}
};
const goReview = async () => {
try {
await recordingRef.current?.stopAndUnloadAsync();
setStatus('reviewing');
} catch {
// Do nothing
}
};
const sendAudio = async () => {
try {
if (!rid) return;
setRecordingAudio(false);
const fileURI = recordingRef.current?.getURI();
const fileData = await getInfoAsync(fileURI as string);
const fileInfo = {
name: `${Date.now()}${RECORDING_EXTENSION}`,
mime: 'audio/aac',
type: 'audio/aac',
store: 'Uploads',
path: fileURI,
size: fileData.exists ? fileData.size : null
} as IUpload;
if (fileInfo) {
if (permissionToUpload) {
await sendFileMessage(rid, fileInfo, tmid, server, user);
}
}
} catch (e) {
log(e);
}
};
if (!rid) {
return null;
}
if (status === 'reviewing') {
return (
<View style={styles.review}>
<View style={styles.audioPlayer}>
<AudioPlayer fileUri={recordingRef.current?.getURI() ?? ''} rid={rid} downloadState='downloaded' />
</View>
<View style={styles.buttons}>
<CancelButton onPress={cancelRecording} />
<View style={{ flex: 1 }} />
<BaseButton
onPress={sendAudio}
testID='message-composer-send'
accessibilityLabel='Send_audio_message'
icon='send-filled'
color={colors.buttonBackgroundPrimaryDefault}
/>
</View>
</View>
);
}
return (
<View style={styles.recording}>
<View style={styles.duration}>
<CustomIcon name='microphone' size={24} color={colors.fontDanger} />
<Duration ref={durationRef} />
</View>
<View style={styles.buttons}>
<CancelButton onPress={cancelRecording} cancelAndDelete />
<View accessible accessibilityLabel={i18n.t('Recording_audio_in_progress')} style={styles.recordingNote}>
<Text style={styles.recordingNoteText}>{i18n.t('Recording_audio_in_progress')}</Text>
</View>
<ReviewButton onPress={goReview} />
</View>
</View>
);
};
function useStyle() {
const { colors } = useTheme();
const style = {
review: {
borderTopWidth: 1,
paddingHorizontal: 16,
paddingBottom: 12,
backgroundColor: colors.surfaceLight,
borderTopColor: colors.strokeLight
},
recording: {
borderTopWidth: 1,
paddingHorizontal: 16,
paddingBottom: 8,
backgroundColor: colors.surfaceLight,
borderTopColor: colors.strokeLight
},
duration: {
flexDirection: 'row',
paddingVertical: 24,
justifyContent: 'center',
alignItems: 'center'
},
audioPlayer: {
flexDirection: 'row',
paddingVertical: 8
},
buttons: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center'
},
recordingNote: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
recordingNoteText: {
fontSize: 14,
...sharedStyles.textRegular,
color: colors.fontSecondaryInfo
}
} as const;
return [style, colors] as const;
}