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!