Skip to content

Instantly share code, notes, and snippets.

@ivanacostarubio
Last active December 28, 2015 17:28
Show Gist options
  • Select an option

  • Save ivanacostarubio/7535622 to your computer and use it in GitHub Desktop.

Select an option

Save ivanacostarubio/7535622 to your computer and use it in GitHub Desktop.
RubyMotion OSX video recording sample
class AppDelegate
attr_accessor :captureMovieFileOutput
def applicationDidFinishLaunching(notification)
buildWindow
end
def buildWindow
@av = AVScreenShackDocument.alloc.init
@mainWindow = NSWindow.alloc.initWithContentRect([[240, 180], [800, 800]],
styleMask: NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask,
backing: NSBackingStoreBuffered,
defer: false)
@mainWindow.title = NSBundle.mainBundle.infoDictionary['CFBundleName']
@mainWindow.orderFrontRegardless
setupVideoBackground()
add_buttons
setup_recording
end
def add_buttons
button = NSButton.alloc.initWithFrame([[0, 0], [100, 32]])
button.title = "Record"
button.action = "record:"
button.target = self
button.bezelStyle = NSRoundedBezelStyle
button.autoresizingMask = NSViewMinXMargin|NSViewMinYMargin
@mainWindow.contentView.addSubview(button)
button = NSButton.alloc.initWithFrame([[200, 0], [100, 32]])
button.title = "Stop"
button.action = "stop:"
button.target = self
button.bezelStyle = NSRoundedBezelStyle
button.autoresizingMask = NSViewMinXMargin|NSViewMinYMargin
@mainWindow.contentView.addSubview(button)
end
def record(sender)
puts "RECORDING"
path = "video_record.mov"
@captureMovieFileOutput.startRecordingToOutputFileURL(NSURL.fileURLWithPath(path), recordingDelegate:self) # <--- Problem
end
def stop(sender)
puts "STOPPING"
captureMovieFileOutput.stopRecording
end
def setupVideoBackground
@vImagePreview = NSView.alloc.initWithFrame(CGRectMake(0,0,600,600))
@vImagePreview.setWantsLayer(true)
@mainWindow.contentView.addSubview(@vImagePreview)
@session = AVCaptureSession.alloc.init
@session.sessionPreset = AVCaptureSessionPresetHigh
viewLayer = @vImagePreview.layer;
captureVideoPreviewLayer = AVCaptureVideoPreviewLayer.alloc.initWithSession(@session)
captureVideoPreviewLayer.frame = @vImagePreview.bounds
@vImagePreview.layer.addSublayer(captureVideoPreviewLayer)
display = CGMainDisplayID()
device = AVCaptureScreenInput.alloc.initWithDisplayID(display)
error_ptr = Pointer.new(:object)
@session.addInput(device)
@session.startRunning()
end
def setup_recording
@captureMovieFileOutput = AVCaptureMovieFileOutput.alloc.init
@captureMovieFileOutput.setDelegate(self)
if @session.canAddOutput(@captureMovieFileOutput)
@session.addOutput(@captureMovieFileOutput)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment