Skip to content

Commit

Permalink
Fix race conditions in iOS (#608)
Browse files Browse the repository at this point in the history
* fix race condition with recordPlayer

* Move all calls to audioRecorder to mainThread
  • Loading branch information
jbaudanza authored Jun 11, 2024
1 parent 7bdf8fc commit 477b63f
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions ios/RNAudioRecorderPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class RNAudioRecorderPlayer: RCTEventEmitter, AVAudioRecorderDelegate {
var audioPlayerAsset: AVURLAsset!
var audioPlayerItem: AVPlayerItem!
var audioPlayer: AVPlayer!
var playTimer: Timer?
var timeObserverToken: Any?

override init() {
Expand Down Expand Up @@ -80,49 +79,52 @@ class RNAudioRecorderPlayer: RCTEventEmitter, AVAudioRecorderDelegate {

@objc(startRecorderTimer)
func startRecorderTimer() -> Void {
DispatchQueue.main.async {
self.recordTimer = Timer.scheduledTimer(
timeInterval: self.subscriptionDuration,
target: self,
selector: #selector(self.updateRecorderProgress),
userInfo: nil,
repeats: true
)
}
let timer = Timer(
timeInterval: self.subscriptionDuration,
target: self,
selector: #selector(self.updateRecorderProgress),
userInfo: nil,
repeats: true
)
RunLoop.main.add(timer, forMode: .default)
self.recordTimer = timer
}

@objc(pauseRecorder:rejecter:)
public func pauseRecorder(
resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) -> Void {
if (audioRecorder == nil) {
return reject("RNAudioPlayerRecorder", "Recorder is not recording", nil)
}

recordTimer?.invalidate()
recordTimer = nil;

audioRecorder.pause()
resolve("Recorder paused!")
DispatchQueue.main.async {
if (self.audioRecorder == nil) {
return reject("RNAudioPlayerRecorder", "Recorder is not recording", nil)
}

self.audioRecorder.pause()
resolve("Recorder paused!")
}
}

@objc(resumeRecorder:rejecter:)
public func resumeRecorder(
resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) -> Void {
if (audioRecorder == nil) {
return reject("RNAudioPlayerRecorder", "Recorder is nil", nil)
}
DispatchQueue.main.async {
if (self.audioRecorder == nil) {
return reject("RNAudioPlayerRecorder", "Recorder is nil", nil)
}

audioRecorder.record()
self.audioRecorder.record()

if (recordTimer == nil) {
startRecorderTimer()
if (self.recordTimer == nil) {
self.startRecorderTimer()
}
resolve("Recorder paused!")
}

resolve("Recorder paused!")
}

@objc
Expand Down Expand Up @@ -332,19 +334,21 @@ class RNAudioRecorderPlayer: RCTEventEmitter, AVAudioRecorderDelegate {
resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) -> Void {
if (audioRecorder == nil) {
reject("RNAudioPlayerRecorder", "Failed to stop recorder. It is already nil.", nil)
return
}

audioRecorder.stop()

if (recordTimer != nil) {
recordTimer!.invalidate()
recordTimer = nil
}

resolve(audioFileURL?.absoluteString)
DispatchQueue.main.async {
if (self.audioRecorder == nil) {
reject("RNAudioPlayerRecorder", "Failed to stop recorder. It is already nil.", nil)
return
}

self.audioRecorder.stop()

resolve(self.audioFileURL?.absoluteString)
}
}

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
Expand Down

0 comments on commit 477b63f

Please sign in to comment.