Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. var mSession = AVCaptureSession()
  2. var mMovieFileOutput = AVCaptureMovieFileOutput()
  3. var mTimer = Timer()
  4. let template1 = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("file") as NSURL
  5. @IBOutlet var playBtn: NSButton!
  6. override func viewDidLoad() {
  7. super.viewDidLoad()
  8. // The template string:
  9. let template = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("file") as NSURL
  10.  
  11. // Fill buffer with a C string representing the local file system path.
  12. var buffer = [Int8](repeating: 0, count: Int(PATH_MAX))
  13. template.getFileSystemRepresentation(&buffer, maxLength: buffer.count)
  14.  
  15. // Create unique file name (and open file):
  16. let fd = mkstemp(&buffer)
  17. if fd != -1 {
  18.  
  19. // Create URL from file system string:
  20. let url = NSURL(fileURLWithFileSystemRepresentation: buffer, isDirectory: false, relativeTo: nil)
  21. print(url.path!)
  22.  
  23. } else {
  24. print("Error: " + String(cString: strerror(errno)))
  25. }
  26.  
  27. // Do any additional setup after loading the view.
  28. }
  29. @IBAction func playBtnAction(_ sender: Any) {
  30. print("hello")
  31. screenRecording(with:template1)
  32. }
  33. override var representedObject: Any? {
  34. didSet {
  35. // Update the view, if already loaded.
  36. }
  37. }
  38.  
  39.  
  40. func screenRecording(with destPath :NSURL)
  41. {
  42. //MARK: - Set the session preset as you wish
  43. mSession.sessionPreset = AVCaptureSessionPresetMedium
  44. // If you're on a multi-display system and you want to capture a secondary display,
  45. // you can call CGGetActiveDisplayList() to get the list of all active displays.
  46. // For this example, we just specify the main display.
  47. // To capture both a main and secondary display at the same time, use two active
  48. // capture sessions, one for each display. On Mac OS X, AVCaptureMovieFileOutput
  49. // only supports writing to a single video track.
  50. var displayId: CGDirectDisplayID = CGMainDisplayID() //kCGDirectMainDisplay
  51. var input = AVCaptureScreenInput(displayID: displayId)
  52. // if !(input != nil) {
  53. // mSession =
  54. // return
  55. // }
  56. if mSession.canAdd(input as? AVAssetWriterInput ?? AVAssetWriterInput()) {
  57. mSession.add(input as? AVAssetWriterInput ?? AVAssetWriterInput())
  58. }
  59. // Create a MovieFileOutput and add it to the session
  60. mMovieFileOutput = AVCaptureMovieFileOutput()
  61. if mSession.canAdd(mMovieFileOutput) {
  62. mSession.add(mMovieFileOutput)
  63. }
  64. // Delete any existing movie file first
  65.  
  66. if FileManager.default.fileExists(atPath: String(describing: destPath) ) {
  67. var err: Error?
  68.  
  69. if (try? FileManager.default.removeItem(atPath: String(describing:destPath))) == nil {
  70. print("Error deleting existing movie (String(describing: err?.localizedDescription))")
  71. }
  72. }
  73.  
  74. mMovieFileOutput.startRecording(toOutputFileURL: destPath as URL!, recordingDelegate: self)
  75. // Fire a timer in 5 seconds
  76. mTimer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.finishRecord), userInfo: nil, repeats: false)
  77. }
  78.  
  79. func finishRecord(_ timer: Timer) {
  80. // Stop recording to the destination movie file
  81. mMovieFileOutput.stopRecording()
  82. mTimer.invalidate()
  83. // mTimer = nil
  84. }
  85.  
  86. // AVCaptureFileOutputRecordingDelegate methods
  87.  
  88. func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {
  89.  
  90. print("Did finish recording to (outputFileURL.description) due to error (error)")
  91. // Stop running the session
  92. mSession.stopRunning()
  93. // Release the session
  94.  
  95. // mSession = nil
  96. // mSession.re
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement