-1

Well, I try to remember currentTime() from AVPlayer in the device memory, for continue playing video after app restart.

I'm making app for watching films from my Raspberry PI. Any ideas?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • `CMTime` in a `struct`, which has a property `seconds` which is a `Double`, but you'd also need the `timeScale` which is just a alias of `Int32`. You could do something fancy and try making `CMTime` conform to `Codable`. You could also use `value`, which is a alias of `Int64` – MadProgrammer Jan 05 '19 at 23:07

1 Answers1

2

Here's an extension to UserDefaults that lets you save and restore a CMTime value:

import CoreMedia

extension UserDefaults {
    func cmtime(forKey key: String) -> CMTime? {
        if let timescale = object(forKey: key + ".timescale") as? NSNumber {
            let seconds = double(forKey: key + ".seconds")
            return CMTime(seconds: seconds, preferredTimescale: timescale.int32Value)
        } else {
            return nil
        }
    }

    func set(_ cmtime: CMTime, forKey key: String) {
        let seconds = cmtime.seconds
        let timescale = cmtime.timescale

        set(seconds, forKey: key + ".seconds")
        set(NSNumber(value: timescale), forKey: key + ".timescale")
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579