GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

AVAudioRecorder and SwiftData

Forums > Swift

Hi all,

I am trying to update my audio recorder app to iOS 17 and was wondering how I might be able to use SwiftData to store audio files so that they can be uploaded to CloudKit. Right now I'm using FileManager, but here is what I have so far:

import Foundation
import AVFoundation

class Recorder: NSObject, ObservableObject, AVAudioRecorderDelegate {
    var audioRecorder: AVAudioRecorder!

    func record() {
        let date = Date()
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "h:mm a, MMM d yyyy"

        let recordingSession = AVAudioSession.sharedInstance()
        do {
            try recordingSession.setCategory(.playAndRecord, mode: .default)
            try recordingSession.setActive(true)
        } catch {
            print("Can not setup the Recording")
        }

        let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let fileName = path.appendingPathComponent("\(dateFormatter.string(from: date)).m4a")

        let settings = [
            AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
            AVSampleRateKey: 44100,
            AVNumberOfChannelsKey: 1,
            AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
        ]

        do {
            audioRecorder = try AVAudioRecorder(url: fileName, settings: settings)
            audioRecorder.prepareToRecord()
            audioRecorder.record()

        } catch {
            print("Failed to Setup the Recording")
        }
    }
    func stop() {
        audioRecorder.stop()
    }

    func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
        if !flag {
            print("Recording failed")
        }
    }
}

And here is the class I made for SwiftData, but haven't used yet:

import Foundation
import SwiftData

@Model class Recording {
    private var name : String
    private var date : Date
    private var url : URL

    init(url: URL) {
        self.name = ""
        self.date = .now
        self.url = url
    }

}

Here is the link to the full repo: https://github.com/aabagdi/MemoMan/tree/main

Any guidance towards the right direction would help immensely, thanks so much in advance!

3      

Hacking with Swift is sponsored by Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.