Skip to content

Commit 0d5372e

Browse files
committed
feat: add progress to onCurrentRecordingWaveformData listener
1 parent 414011f commit 0d5372e

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

android/src/main/java/com/audiowaveform/AudioWaveformModule.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
108108
}
109109

110110
try {
111+
stopEmittingRecorderValue()
111112
val currentTime = System.currentTimeMillis()
112113
if (currentTime - startTime < 500) {
113114
promise.reject("SHORT_RECORDING", "Recording is too short")
114115
return
115116
}
116117

117-
stopEmittingRecorderValue()
118118
audioRecorder.stopRecording(recorder, path!!, promise)
119119
recorder = null
120120
path = null
@@ -414,6 +414,9 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
414414
override fun run() {
415415
val currentDecibel = getDecibel()
416416
val args: WritableMap = Arguments.createMap()
417+
val currentTime = System.currentTimeMillis()
418+
val progress = currentTime - startTime
419+
args.putInt(Constants.progress, progress.toInt())
417420
if (currentDecibel == Double.NEGATIVE_INFINITY) {
418421
args.putDouble(Constants.currentDecibel, 0.0)
419422
} else {

ios/AudioRecorder.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class AudioRecorder: NSObject, AVAudioRecorderDelegate{
1717
var recordedDuration: CMTime = CMTime.zero
1818
private var timer: Timer?
1919
var updateFrequency = UpdateFrequency.medium
20+
var startTime: DispatchTime?
2021

2122
private func createAudioRecordPath(fileNameFormat: String?) -> URL? {
2223
let format = DateFormatter()
@@ -70,6 +71,7 @@ public class AudioRecorder: NSObject, AVAudioRecorderDelegate{
7071
audioRecorder?.isMeteringEnabled = true
7172
audioRecorder?.record()
7273
startListening()
74+
startTime = DispatchTime.now() // Initialize startTime
7375
resolve(true)
7476
} catch let error as NSError {
7577
print(error.localizedDescription)
@@ -79,7 +81,9 @@ public class AudioRecorder: NSObject, AVAudioRecorderDelegate{
7981

8082
@objc func timerUpdate(_ sender:Timer) {
8183
if (audioRecorder?.isRecording ?? false) {
82-
EventEmitter.sharedInstance.dispatch(name: Constants.onCurrentRecordingWaveformData, body: [Constants.currentDecibel: getDecibelLevel()])
84+
let currentTime = DispatchTime.now()
85+
let progress = Double(currentTime.uptimeNanoseconds - startTime!.uptimeNanoseconds) / 1_000_000 // Convert to millisecond
86+
EventEmitter.sharedInstance.dispatch(name: Constants.onCurrentRecordingWaveformData, body: [Constants.currentDecibel: getDecibelLevel(), Constants.progress: progress])
8387
}
8488
}
8589

src/types/AudioWaveformTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export interface IOnCurrentExtractedWaveForm extends IPlayerKey {
6969

7070
export interface IOnCurrentRecordingWaveForm {
7171
currentDecibel: number;
72+
progress: number;
7273
}
7374

7475
export interface ISetPlaybackSpeed extends IPlayerKey {

0 commit comments

Comments
 (0)