Cotton code
46 lines · 2.2 KB · SwiftRaw
// How long a recording on this device actually runs — asked of the BYTES.//// The recorder banks its elapsed figure at transitions (start, pause, resume,// stop), which is right for a recording that ends at Stop and stale for one the// OS killed while it was running: the marker then says a second and the audio// says thirteen (the walk of 15 Sep 2026). The file cannot be stale, so the// recovery slip and the meeting it creates both read it here.//// **`AVAudioPlayer` rather than `AVURLAsset`**, for two reasons and neither is// taste:////  1. it answers SYNCHRONOUSLY. `AVURLAsset.duration` is deprecated in favour//     of the async `load(.duration)`, and the rule this answers — what the//     offer says, and what is sent to `meetings.create` — is a pure function//     over a marker (`MeetingRecovery.length(of:probing:)`) with one injected//     probe. A synchronous probe keeps it one.//  2. it REFUSES a file it cannot read rather than answering zero. An MPEG-4//     recording whose moov atom was never written — the unfinalised case, which//     is most of what this looks at — makes no player at all, so `nil` comes//     back and the marker's own figure stands. A player that answered `0` would//     read as "an empty recording" over bytes there is something in.//// It opens the file; it does not read the body into memory (`AVAudioPlayer(data:)`// is the spelling that would, and it is not this one).import AVFoundationimport Foundation/// The length of the audio at a path, in milliseconds.enum MeetingAudioLength {    /// What the file says, or `nil` when it cannot say.    ///    /// Every failure is the same answer: no path, no file, a container the    /// player will not open, an unfinalised one, a duration that is not a    /// positive number. The caller has a fallback for all of them and needs no    /// reason for any.    static func milliseconds(atPath path: String) -> Int? {        guard !path.isEmpty else { return nil }        guard let player = try? AVAudioPlayer(contentsOf: URL(fileURLWithPath: path)) else {            return nil        }        let seconds = player.duration        guard seconds.isFinite, seconds > 0 else { return nil }        return Int((seconds * 1000).rounded())    }}