TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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

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

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

        // The user grants access. Present recording interface.
        let recordingSession = AVAudioSession.sharedInstance()
        do {
            try recordingSession.setCategory(.playAndRecord, mode: .default)
            try recordingSession.setActive(true)
        } catch {
            throw Errors.FailedToInitSessionError
        }
        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 {
            throw Errors.FailedToRecordError
        }
    }

    func stop() {
        audioRecorder.stop()
    }

    nonisolated 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!

   

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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

Reply to this topic…

You need to create an account or log in to reply.

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.