-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.jsx
More file actions
138 lines (123 loc) · 4.05 KB
/
App.jsx
File metadata and controls
138 lines (123 loc) · 4.05 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
import React, {useCallback, useEffect, useState} from 'react';
import { View, Text, TouchableOpacity, StyleSheet, Alert } from 'react-native';
import AudioRecorderPlayer from 'react-native-audio-recorder-player';
import {debounce} from 'lodash';
const audioRecorderPlayer = new AudioRecorderPlayer();
const App = () => {
const [isRecording, setIsRecording] = useState(false);
const [hasRecorded, setHasRecorded] = useState(false);
const [isPlaying, setIsPlaying] = useState(false);
// ========= life-cycle hooks =========
useEffect(() => {
audioRecorderPlayer.addRecordBackListener((e) => {});
audioRecorderPlayer.addPlayBackListener(playBackListener);
return () => {
audioRecorderPlayer.removeRecordBackListener();
audioRecorderPlayer.removePlayBackListener();
}
}, []);
// ========= 이벤트 핸들러 =========
const playBackListener = useCallback((e) => {
if (Math.abs(e.currentPosition - e.duration) < 500) {
audioRecorderPlayer.stopPlayer().then(() => {
console.log("[재생 완료]");
setIsPlaying(false);
audioRecorderPlayer.removePlayBackListener()
}).catch((error) => {
console.error("재생 중지 실패:", error);
Alert.alert("재생을 중지할 수 없습니다.", error.message);
});
}
}, []);
// ================= 녹음 시작 ====================
const onStartRecord = async () => {
setIsRecording(true);
try {
const result = await audioRecorderPlayer.startRecorder(); // 오디오 녹음 시작
console.log("[녹음 시작] 경로 : " + result);
setHasRecorded(false);
} catch (error) {
console.error("녹음 시작 실패:", error);
Alert.alert("녹음을 시작할 수 없습니다.", error.message);
setIsRecording(false);
}
};
// ================= 저장 ====================
const onStopRecord = async () => {
try {
const result = await audioRecorderPlayer.stopRecorder();
console.log("[녹음 완료] 경로 : " + result);
setIsRecording(false);
setHasRecorded(true);
} catch (error) {
console.error("녹음 중지 실패:", error);
Alert.alert("녹음을 중지할 수 없습니다.", error.message);
}
};
// ================= 재생 ====================
const onStartPlay = async () => {
setIsPlaying(true);
try {
await audioRecorderPlayer.startPlayer();
audioRecorderPlayer.addPlayBackListener(playBackListener);
} catch (error) {
console.error("재생 시작 실패:", error);
Alert.alert("재생할 수 없습니다.", error.message);
setIsPlaying(false);
}
};
const debouncedStopAndPlay = debounce(async () => {
await onStopRecord();
await onStartPlay();
}, 400); // 300ms 디바운스
// ================= 렌더링 ====================
return (
<View style={styles.container}>
<TouchableOpacity
disabled={isRecording || isPlaying}
style={isRecording ? styles.buttonRecording : isPlaying ? styles.buttonPlaying : styles.button}
onPressIn={onStartRecord}
onPressOut={debouncedStopAndPlay}
>
<Text style={styles.btnText}>{isRecording ? 'Recording' : isPlaying ? 'Playing' : 'Record'}</Text>
</TouchableOpacity>
</View>
);
};
// ================= 스타일 ====================
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
btnText:{
fontSize: 30,
color: 'white',
},
button: {
width: 200,
height: 200,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgb(221,221,221)',
borderRadius: 100,
},
buttonRecording: {
width: 200,
height: 200,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgba(255,68,68,0.62)',
borderRadius: 100,
},
buttonPlaying: {
width: 200,
height: 200,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgba(16,169,0,0.38)',
borderRadius: 100,
}
});
export default App;