Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mSession = AVCaptureSession()
- var mMovieFileOutput = AVCaptureMovieFileOutput()
- var mTimer = Timer()
- let template1 = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("file") as NSURL
- @IBOutlet var playBtn: NSButton!
- override func viewDidLoad() {
- super.viewDidLoad()
- // The template string:
- let template = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("file") as NSURL
- // Fill buffer with a C string representing the local file system path.
- var buffer = [Int8](repeating: 0, count: Int(PATH_MAX))
- template.getFileSystemRepresentation(&buffer, maxLength: buffer.count)
- // Create unique file name (and open file):
- let fd = mkstemp(&buffer)
- if fd != -1 {
- // Create URL from file system string:
- let url = NSURL(fileURLWithFileSystemRepresentation: buffer, isDirectory: false, relativeTo: nil)
- print(url.path!)
- } else {
- print("Error: " + String(cString: strerror(errno)))
- }
- // Do any additional setup after loading the view.
- }
- @IBAction func playBtnAction(_ sender: Any) {
- print("hello")
- screenRecording(with:template1)
- }
- override var representedObject: Any? {
- didSet {
- // Update the view, if already loaded.
- }
- }
- func screenRecording(with destPath :NSURL)
- {
- //MARK: - Set the session preset as you wish
- mSession.sessionPreset = AVCaptureSessionPresetMedium
- // If you're on a multi-display system and you want to capture a secondary display,
- // you can call CGGetActiveDisplayList() to get the list of all active displays.
- // For this example, we just specify the main display.
- // To capture both a main and secondary display at the same time, use two active
- // capture sessions, one for each display. On Mac OS X, AVCaptureMovieFileOutput
- // only supports writing to a single video track.
- var displayId: CGDirectDisplayID = CGMainDisplayID() //kCGDirectMainDisplay
- var input = AVCaptureScreenInput(displayID: displayId)
- // if !(input != nil) {
- // mSession =
- // return
- // }
- if mSession.canAdd(input as? AVAssetWriterInput ?? AVAssetWriterInput()) {
- mSession.add(input as? AVAssetWriterInput ?? AVAssetWriterInput())
- }
- // Create a MovieFileOutput and add it to the session
- mMovieFileOutput = AVCaptureMovieFileOutput()
- if mSession.canAdd(mMovieFileOutput) {
- mSession.add(mMovieFileOutput)
- }
- // Delete any existing movie file first
- if FileManager.default.fileExists(atPath: String(describing: destPath) ) {
- var err: Error?
- if (try? FileManager.default.removeItem(atPath: String(describing:destPath))) == nil {
- print("Error deleting existing movie (String(describing: err?.localizedDescription))")
- }
- }
- mMovieFileOutput.startRecording(toOutputFileURL: destPath as URL!, recordingDelegate: self)
- // Fire a timer in 5 seconds
- mTimer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.finishRecord), userInfo: nil, repeats: false)
- }
- func finishRecord(_ timer: Timer) {
- // Stop recording to the destination movie file
- mMovieFileOutput.stopRecording()
- mTimer.invalidate()
- // mTimer = nil
- }
- // AVCaptureFileOutputRecordingDelegate methods
- func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {
- print("Did finish recording to (outputFileURL.description) due to error (error)")
- // Stop running the session
- mSession.stopRunning()
- // Release the session
- // mSession = nil
- // mSession.re
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement