Trouble looping with AVFoundation audio recorder & audioplayer

Viewed 110

I am trying to make an app that records and loops an audio clip. I have this function in my AudioPlayer struct:

func loop(nums:Int){
        audioPlayer!.numberOfLoops = nums
    }

And I am referencing it in my swiftUI struct. For some reason it crashes the app. Here is where I reference the function:

Button(action:{
                if !canRecord && !edit && slot.beenRecorded == true{
                    if isInfinite{
                        audioPlayer.loop(nums: -1)
                    }
                    else{
                        audioPlayer.loop(nums: myInt)
                    }
                    if let recording = audioRecorder.recordings.first(where: { $0.fileURL.lastPathComponent == "\(index).m4a" }) {
                        self.audioPlayer.startPlayback(audio: recording.fileURL)
                    }
                    
                    else {
                        print("No audio url was saved")
                    }
                    
                    if audioPlayer.isPlaying == false {
                        print("audio is playing")
                    }
                    
                }
                if canRecord && oneIsRecording == false{
                    slot.beenRecorded = true
                    UserDefaults.standard.set(true, forKey: slot.id)
                    isActive = true
                }
            })
0 Answers
Related