Hello all,
I have an AudioRecorder class where users can record and playback audio using the AVFoundation framework. I'm trying to give users the option to share the m4a audio recording using UIActivityViewController. This share screen needs some data to be shared, in this form:
I understand that you can put strings and URL's in this spot, but I'm confused as how to put the user's audio recording in this place. Here is the code for the audio recorder, as well as how the audio file is saved. I'm pretty new to the AVFoundation framework and working with files.
So ultimately, my question is: what should I put in the let data = part?
Thank you so much!
I have an AudioRecorder class where users can record and playback audio using the AVFoundation framework. I'm trying to give users the option to share the m4a audio recording using UIActivityViewController. This share screen needs some data to be shared, in this form:
Code Block let data = // some data
I understand that you can put strings and URL's in this spot, but I'm confused as how to put the user's audio recording in this place. Here is the code for the audio recorder, as well as how the audio file is saved. I'm pretty new to the AVFoundation framework and working with files.
Code Block class AudioRecorder: NSObject, ObservableObject { override init() { super.init() fetchRecordings() } let objectWillChange = PassthroughSubject<AudioRecorder, Never>() var audioRecorder: AVAudioRecorder! var recordings = [Recording]() var recording = false { didSet { objectWillChange.send(self) } } func startRecording() { let recordingSession = AVAudioSession.sharedInstance() do { try recordingSession.setCategory(.playAndRecord, mode: .default) try recordingSession.setActive(true) } catch { print("Failed to set up recording session") } let documentPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] let audioFilename = documentPath.appendingPathComponent("\(Date().toString(dateFormat: "dd-MM-YY 'at' HH:mm")).m4a") let settings = [ AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey: 12000, AVNumberOfChannelsKey: 1, AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue ] do { audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings) audioRecorder.record() recording = true } catch { print("Could not start recording") } } func stopRecording() { audioRecorder.stop() recording = false fetchRecordings() } func fetchRecordings() { recordings.removeAll() let fileManager = FileManager.default let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] let directoryContents = try! fileManager.contentsOfDirectory(at: documentDirectory, includingPropertiesForKeys: nil) for audio in directoryContents { let recording = Recording(fileURL: audio, createdAt: getCreationDate(for: audio)) recordings.append(recording) } recordings.sort(by: { $0.createdAt.compare($1.createdAt) == .orderedAscending}) objectWillChange.send(self) } func getCreationDate(for file: URL) -> Date { if let attributes = try? FileManager.default.attributesOfItem(atPath: file.path) as [FileAttributeKey: Any], let creationDate = attributes[FileAttributeKey.creationDate] as? Date { return creationDate } else { return Date() } } func deleteRecording(urlsToDelete: [URL]) { for url in urlsToDelete { print(url) do { try FileManager.default.removeItem(at: url) } catch { print("File could not be deleted!") } } fetchRecordings() } }
So ultimately, my question is: what should I put in the let data = part?
Thank you so much!