Getting The operation couldn’t be completed. (OSStatus error 2003334207.)

Hello! I am writing an app that let band students record their practice sessions and be able to play them back. I'm not the most experienced developer, but working towards getting there. When trying to play back what I recorded after selecting the cell, I get this error The operation couldn’t be completed. (OSStatus error 2003334207.) in the console. Thank you in advance for help! Here is my code:

import AVFoundation
import UIKit

class ViewController: UIViewController, AVAudioRecorderDelegate, UITableViewDelegate, UITableViewDataSource {
    
    var recordingSession: AVAudioSession!
    var audioRecorder: AVAudioRecorder!
    var audioPlayer: AVAudioPlayer!
    
    var numberOfRecorders: Int = 0

    
    @IBOutlet weak var myTableView: UITableView!
    
    @IBOutlet weak var startRecordingButton: UIButton!
    
    @IBAction func startRecording(_ sender: UIButton) {
        //Check if we have an active recorder
        if audioRecorder == nil  {
            numberOfRecorders += 1
            let fileName = getDirectory().appendingPathComponent("\(numberOfRecorders).m4a")
            
            let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                            AVSampleRateKey: 1200,
                            AVNumberOfChannelsKey: 1,
                            AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue]
            
            //Start Recording
            do {
                audioRecorder = try AVAudioRecorder(url: fileName, settings: settings)
                audioRecorder.delegate = self
                audioRecorder.record()
                
                startRecordingButton.setTitle("Stop Recording", for: .normal)
            } catch {
                displayAlert(title: "Oop", message: "Recording Failed")
            }
        } else {
            //Stopping audio recording
            audioRecorder.stop()
            audioRecorder = nil
            
            UserDefaults.standard.set(numberOfRecorders, forKey: "myNumber")
            myTableView.reloadData()
            
            startRecordingButton.setTitle("Start Recording", for: .normal)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        recordingSession = AVAudioSession.sharedInstance()
        
        if let number: Int = UserDefaults.standard.object(forKey: "myNumber") as? Int {
            numberOfRecorders = number
        }
        
        AVAudioSession.sharedInstance().requestRecordPermission() {
            [weak self] isGranted in
            
            guard let strongSelf = self else {return}
            
            guard isGranted else {
                let settingURL = URL(string: UIApplication.openSettingsURLString)!
                UIApplication.shared.open(settingURL, options: [:], completionHandler: nil)
                
                return
            }
        }
    }
    //Function that gets path to directory
    func getDirectory() -> URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let documentDirectory = paths[0]
        return documentDirectory
    }
    
    //Function that displays an alert
    
    func displayAlert(title: String, message: String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "dismiss", style: .default, handler: nil))
        present(alert, animated: true, completion: nil)
    }
    
    //MARK: - Tableview Setup
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return numberOfRecorders
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = String(indexPath.row + 1)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let path = getDirectory().appendingPathComponent("\(indexPath.row + 1).m4a")
        
        do {
           // Set up the AVAudioSession configuration
           try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default)
           try AVAudioSession.sharedInstance().setActive(true)

           // Keep an instance of AVAudioPlayer at the UIViewController level
            self.audioPlayer = try AVAudioPlayer(contentsOf: getDirectory())
           audioPlayer.play()
         } catch let error {
           print(error.localizedDescription)
         }
    }
    
}

Answered by Dirk-FU in 748427022

I don't know the reason for your error, but perhaps this very current article helps you to find it: https://www.avanderlee.com/swift/operation-couldnt-completed-error-code/

Accepted Answer

I don't know the reason for your error, but perhaps this very current article helps you to find it: https://www.avanderlee.com/swift/operation-couldnt-completed-error-code/

Getting The operation couldn’t be completed. (OSStatus error 2003334207.)
 
 
Q