Drop `.onError()`
Better to just have one callback for when it's done.
This commit is contained in:
parent
a9a53cbb53
commit
ef9d1b0030
|
@ -2,6 +2,10 @@ import Foundation
|
||||||
import AVFoundation
|
import AVFoundation
|
||||||
import Aperture
|
import Aperture
|
||||||
|
|
||||||
|
func delay(seconds: TimeInterval, closure: @escaping () -> Void) {
|
||||||
|
DispatchQueue.main.asyncAfter(deadline: .now() + seconds, execute: closure)
|
||||||
|
}
|
||||||
|
|
||||||
guard
|
guard
|
||||||
let deviceInfo = Aperture.Devices.iOS().first,
|
let deviceInfo = Aperture.Devices.iOS().first,
|
||||||
let device = AVCaptureDevice(uniqueID: deviceInfo.id)
|
let device = AVCaptureDevice(uniqueID: deviceInfo.id)
|
||||||
|
@ -17,17 +21,23 @@ guard let aperture = try? Aperture(destination: url, iosDevice: device) else {
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
aperture.onError = {
|
aperture.onFinish = {
|
||||||
print($0)
|
if let error = $0 {
|
||||||
|
print(error)
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print("Finished recording:", url.path)
|
||||||
|
exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
aperture.start()
|
aperture.start()
|
||||||
|
|
||||||
print("Recording the screen of “\(deviceInfo.name)” for 5 seconds")
|
print("Recording the screen of “\(deviceInfo.name)” for 5 seconds")
|
||||||
|
|
||||||
sleep(5)
|
delay(seconds: 5) {
|
||||||
|
|
||||||
aperture.stop()
|
aperture.stop()
|
||||||
|
}
|
||||||
|
|
||||||
print("Finished recording:", url.path)
|
setbuf(__stdoutp, nil)
|
||||||
|
RunLoop.current.run()
|
||||||
|
|
|
@ -16,8 +16,7 @@ public final class Aperture: NSObject {
|
||||||
private var activity: NSObjectProtocol?
|
private var activity: NSObjectProtocol?
|
||||||
|
|
||||||
public var onStart: (() -> Void)?
|
public var onStart: (() -> Void)?
|
||||||
public var onFinish: (() -> Void)?
|
public var onFinish: ((Swift.Error?) -> Void)?
|
||||||
public var onError: ((Swift.Error) -> Void)?
|
|
||||||
public var onPause: (() -> Void)?
|
public var onPause: (() -> Void)?
|
||||||
public var onResume: (() -> Void)?
|
public var onResume: (() -> Void)?
|
||||||
public var isRecording: Bool { output.isRecording }
|
public var isRecording: Bool { output.isRecording }
|
||||||
|
@ -158,7 +157,11 @@ public final class Aperture: NSObject {
|
||||||
|
|
||||||
public func stop() {
|
public func stop() {
|
||||||
output.stopRecording()
|
output.stopRecording()
|
||||||
session.stopRunning()
|
|
||||||
|
// This prevents a race condition in Apple's APIs with the above and below calls.
|
||||||
|
sleep(for: 0.1)
|
||||||
|
|
||||||
|
self.session.stopRunning()
|
||||||
}
|
}
|
||||||
|
|
||||||
public func pause() {
|
public func pause() {
|
||||||
|
@ -190,14 +193,7 @@ extension Aperture: AVCaptureFileOutputRecordingDelegate {
|
||||||
|
|
||||||
public func fileOutput(_ captureOutput: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Swift.Error?) {
|
public func fileOutput(_ captureOutput: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Swift.Error?) {
|
||||||
shouldPreventSleep = false
|
shouldPreventSleep = false
|
||||||
|
onFinish?(error)
|
||||||
let FINISHED_RECORDING_ERROR_CODE = -11_806
|
|
||||||
|
|
||||||
if let error = error, error._code != FINISHED_RECORDING_ERROR_CODE {
|
|
||||||
onError?(error)
|
|
||||||
} else {
|
|
||||||
onFinish?()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func fileOutput(_ output: AVCaptureFileOutput, didPauseRecordingTo fileURL: URL, from connections: [AVCaptureConnection]) {
|
public func fileOutput(_ output: AVCaptureFileOutput, didPauseRecordingTo fileURL: URL, from connections: [AVCaptureConnection]) {
|
||||||
|
|
|
@ -82,3 +82,8 @@ extension Optional {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func sleep(for duration: TimeInterval) {
|
||||||
|
usleep(useconds_t(duration * Double(USEC_PER_SEC)))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue