@@ -31,6 +31,7 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
3131 private var sampleRate: Int = 44100
3232 private var bitRate: Int = 128000
3333 private val handler = Handler (Looper .getMainLooper())
34+ private var startTime: Long = 0
3435
3536 companion object {
3637 const val NAME = " AudioWaveform"
@@ -41,6 +42,20 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
4142 return NAME
4243 }
4344
45+ @ReactMethod
46+ fun markPlayerAsUnmounted () {
47+ if (audioPlayers.isEmpty()) {
48+ return
49+ }
50+
51+ audioPlayers.values.forEach { player ->
52+ if (player != null ) {
53+ player.markPlayerAsUnmounted()
54+ }
55+ }
56+ }
57+
58+
4459 @ReactMethod
4560 fun checkHasAudioRecorderPermission (promise : Promise ) {
4661 audioRecorder.checkPermission(currentActivity, promise)
@@ -66,6 +81,7 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
6681 initRecorder(obj, promise)
6782 val useLegacyNormalization = true
6883 audioRecorder.startRecorder(recorder, useLegacyNormalization, promise)
84+ startTime = System .currentTimeMillis() // Initialize startTime
6985 startEmittingRecorderValue()
7086 }
7187
@@ -90,10 +106,21 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
90106 return
91107 }
92108
93- audioRecorder.stopRecording(recorder, path!! , promise)
94- stopEmittingRecorderValue()
95- recorder = null
96- path = null
109+ try {
110+ val currentTime = System .currentTimeMillis()
111+ if (currentTime - startTime < 500 ) {
112+ promise.reject(" SHORT_RECORDING" , " Recording is too short" )
113+ return
114+ }
115+
116+ stopEmittingRecorderValue()
117+ audioRecorder.stopRecording(recorder, path!! , promise)
118+ recorder = null
119+ path = null
120+ } catch (e: Exception ) {
121+ Log .e(Constants .LOG_TAG , " Failed to stop recording" , e)
122+ promise.reject(" Error" , " Failed to stop recording: ${e.message} " )
123+ }
97124 }
98125
99126 @ReactMethod
@@ -145,8 +172,9 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
145172 fun stopPlayer (obj : ReadableMap , promise : Promise ) {
146173 val key = obj.getString(Constants .playerKey)
147174 if (key != null ) {
148- audioPlayers[key]?.stop(promise )
175+ audioPlayers[key]?.stop()
149176 audioPlayers[key] = null // Release the player after stopping it
177+ promise.resolve(true )
150178 } else {
151179 promise.reject(" stopPlayer Error" , " Player key can't be null" )
152180 }
@@ -164,19 +192,24 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
164192
165193 @ReactMethod
166194 fun seekToPlayer (obj : ReadableMap , promise : Promise ) {
167- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
168- val progress = obj.getInt(Constants .progress)
169- val key = obj.getString(Constants .playerKey)
170- if (key != null ) {
171- audioPlayers[key]?.seekToPosition(progress.toLong(), promise)
195+ try {
196+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
197+ val progress = obj.getInt(Constants .progress)
198+ val key = obj.getString(Constants .playerKey)
199+ if (key != null ) {
200+ audioPlayers[key]?.seekToPosition(progress.toLong(), promise)
201+ } else {
202+ promise.reject(" seekTo Error" , " Player key can't be null" )
203+ }
172204 } else {
173- promise.reject(" seekTo Error" , " Player key can't be null" )
205+ Log .e(
206+ Constants .LOG_TAG ,
207+ " Minimum android O is required for seekTo function to works"
208+ )
209+ promise.resolve(false )
174210 }
175- } else {
176- Log .e(
177- Constants .LOG_TAG ,
178- " Minimum android O is required for seekTo function to works"
179- )
211+ } catch (e: Exception ) {
212+ promise.reject(" seekTo Error" , e.toString())
180213 }
181214 }
182215
@@ -218,25 +251,31 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
218251 @ReactMethod
219252 fun stopAllPlayers (promise : Promise ) {
220253 for ((key, _) in audioPlayers) {
221- audioPlayers[key]?.stop(promise )
254+ audioPlayers[key]?.stop()
222255 audioPlayers[key] = null
223256 }
257+ promise.resolve(true )
224258 }
225259
226260 @ReactMethod
227261 fun setPlaybackSpeed (obj : ReadableMap , promise : Promise ) {
228- // If the key doesn't exist or if the value is null or undefined, set default speed to 1.0
229- val speed = if (! obj.hasKey(Constants .speed) || obj.isNull(Constants .speed)) {
230- 1.0f // Set default speed to 1.0 if null, undefined, or missing
231- } else {
232- obj.getDouble(Constants .speed).toFloat()
233- }
262+ try {
263+ // If the key doesn't exist or if the value is null or undefined, set default speed to 1.0
264+ val speed = if (! obj.hasKey(Constants .speed) || obj.isNull(Constants .speed)) {
265+ 1.0f // Set default speed to 1.0 if null, undefined, or missing
266+ } else {
267+ obj.getDouble(Constants .speed).toFloat()
268+ }
234269
235- val key = obj.getString(Constants .playerKey)
236- if (key != null ) {
237- audioPlayers[key]?.setPlaybackSpeed(speed, promise)
238- } else {
239- promise.reject(" setPlaybackSpeed Error" , " Player key can't be null" )
270+ val key = obj.getString(Constants .playerKey)
271+ if (key != null ) {
272+ val status = audioPlayers[key]?.setPlaybackSpeed(speed)
273+ promise.resolve(status ? : false )
274+ } else {
275+ promise.reject(" setPlaybackSpeed Error" , " Player key can't be null" )
276+ }
277+ } catch (e: Exception ) {
278+ promise.reject(" setPlaybackSpeed Error" , e.toString())
240279 }
241280 }
242281
@@ -276,11 +315,15 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
276315 }
277316 }
278317 }
279- },
280- promise
318+ override fun onReject (error : String? , message : String? ) {
319+ promise.reject(error, message)
320+ }
321+ override fun onResolve (value : MutableList <MutableList <Float >>) {
322+ promise.resolve(Arguments .fromList(value))
323+ }
324+ }
281325 )
282- extractors[playerKey]?.startDecode()
283- extractors[playerKey]?.stop()
326+ extractors[playerKey]?.startDecode();
284327 }
285328
286329 private fun normalizeWaveformData (data : MutableList <Float >, scale : Float = 0.25f, threshold : Float = 0.01f): MutableList <Float > {
0 commit comments