@@ -24,6 +24,7 @@ class Recorder private constructor(context: Context) {
2424 private val appContext = context
2525 private var withFFmpegMode = false
2626 private var withDebug = false
27+ private var withCancel = false
2728 private var withRefreshTimerMillis: Long = AudioConstants .SUBSCRIPTION_DURATION_IN_MILLISECONDS
2829
2930 private lateinit var recorder: AudioRecorder
@@ -56,59 +57,61 @@ class Recorder private constructor(context: Context) {
5657 this .convertConfig = convertConfig
5758
5859 this .source = when (sourceMode) {
59- " noise" -> NoiseAudioSource (this .config)
60- " auto" -> AutomaticGainAudioSource (this .config)
61- else -> DefaultAudioSource (this .config)
62- }
63- return this
60+ " noise" -> NoiseAudioSource (this .config)
61+ " auto" -> AutomaticGainAudioSource (this .config)
62+ else -> DefaultAudioSource (this .config)
63+ }
64+ return this
6465 }
6566
6667 fun setSource (@NonNull filePath : String ) {
67- if (! this ::recorder.isInitialized)
68- throw Exception (Constant .NOT_INIT_RECORDER )
69-
70- sourceFilePath = appContext.recordFile(filePath)
71-
72- recorder.create(FFmpegRecordFinder ::class .java) {
73- this .ffmpegMode = withFFmpegMode
74- this .destFile = sourceFilePath
75- this .recordConfig = config
76- this .audioSource = source
77- this .refreshTimerMillis = withRefreshTimerMillis
78- this .chunkAvailableCallback = onRawBuffer
79- this .silentDetectedCallback = onSilentDetected
80- this .timerCountCallback = onProgress
81- this .debugMode = withDebug
82- }
68+ if (! this ::recorder.isInitialized)
69+ throw Exception (Constant .NOT_INIT_RECORDER )
70+
71+ withCancel = false
72+ sourceFilePath = appContext.recordFile(filePath)
73+ recorder.create(FFmpegRecordFinder ::class .java) {
74+ this .ffmpegMode = withFFmpegMode
75+ this .destFile = sourceFilePath
76+ this .recordConfig = config
77+ this .audioSource = source
78+ this .refreshTimerMillis = withRefreshTimerMillis
79+ this .chunkAvailableCallback = onRawBuffer
80+ this .silentDetectedCallback = onSilentDetected
81+ this .timerCountCallback = onProgress
82+ this .debugMode = withDebug
83+ }
8384
84- if (withFFmpegMode) {
85- val ffmpegRecorder: FFmpegAudioRecorder =
86- recorder.getAudioRecorder() as ? FFmpegAudioRecorder ? : return
87- ffmpegRecorder.setContext(appContext)
88- ffmpegRecorder.setConvertConfig(convertConfig)
89- ffmpegRecorder.setOnConvertStateChangeListener {
90- onFFmpegState?.invoke(it)
91- if (it == FFmpegConvertState .SUCCESS ) {
92- finishRecording()
93- }
94- }
85+ if (withFFmpegMode) {
86+ val ffmpegRecorder: FFmpegAudioRecorder =
87+ recorder.getAudioRecorder() as ? FFmpegAudioRecorder ? : return
88+ ffmpegRecorder.setContext(appContext)
89+ ffmpegRecorder.setConvertConfig(convertConfig)
90+ ffmpegRecorder.setOnConvertStateChangeListener {
91+ onFFmpegState?.invoke(it)
92+ if (it == FFmpegConvertState .SUCCESS && ! withCancel) {
93+ finishRecording()
94+ }
9595 }
96+ }
9697
97- recorder.setOnRecordStateChangeListener { onRecordState?.invoke(it) }
98+ recorder.setOnRecordStateChangeListener { onRecordState?.invoke(it) }
9899 }
99100
100101 fun startRecording () {
101- if (! this ::recorder.isInitialized)
102- throw Exception (Constant .NOT_INIT_RECORDER )
102+ if (! this ::recorder.isInitialized)
103+ throw Exception (Constant .NOT_INIT_RECORDER )
103104
105+ withCancel = false
104106 if (! recorder.isRecording())
105107 recorder.startRecording()
106108 }
107109
108110 fun stopRecording () {
109- if (! this ::recorder.isInitialized)
110- throw Exception (Constant .NOT_INIT_RECORDER )
111+ if (! this ::recorder.isInitialized)
112+ throw Exception (Constant .NOT_INIT_RECORDER )
111113
114+ withCancel = false
112115 if (recorder.isRecording()) {
113116 recorder.stopRecording()
114117 if (! withFFmpegMode) {
@@ -118,21 +121,33 @@ class Recorder private constructor(context: Context) {
118121 }
119122
120123 fun resumeRecording () {
121- if (! this ::recorder.isInitialized)
122- throw Exception (Constant .NOT_INIT_RECORDER )
124+ if (! this ::recorder.isInitialized)
125+ throw Exception (Constant .NOT_INIT_RECORDER )
123126
127+ withCancel = false
124128 if (! recorder.isRecording())
125129 recorder.resumeRecording()
126130 }
127131
128132 fun pauseRecording () {
129- if (! this ::recorder.isInitialized)
130- throw Exception (Constant .NOT_INIT_RECORDER )
133+ if (! this ::recorder.isInitialized)
134+ throw Exception (Constant .NOT_INIT_RECORDER )
131135
136+ withCancel = false
132137 if (recorder.isRecording())
133138 recorder.pauseRecording()
134139 }
135140
141+ fun cancelRecording () {
142+ if (! this ::recorder.isInitialized)
143+ throw Exception (Constant .NOT_INIT_RECORDER )
144+
145+ withCancel = true
146+ if (recorder.isRecording()) {
147+ recorder.stopRecording()
148+ }
149+ }
150+
136151 private fun finishRecording () {
137152 val recordMetadata = recorder.retrieveMetadata(sourceFilePath ? : File (" " ))
138153 onFinished?.invoke(sourceFilePath, recordMetadata)
0 commit comments