Skip to content

Commit

Permalink
Add ability to pause/resume
Browse files Browse the repository at this point in the history
  • Loading branch information
vishaltelangre committed Oct 22, 2016
1 parent a42682f commit 690d7f1
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 304 deletions.
6 changes: 6 additions & 0 deletions Kyapchar/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
21 changes: 21 additions & 0 deletions Kyapchar/Assets.xcassets/pause.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "pause.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Binary file added Kyapchar/Assets.xcassets/pause.imageset/pause.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions Kyapchar/Assets.xcassets/record.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "record.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions Kyapchar/Assets.xcassets/resume.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "resume.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions Kyapchar/Assets.xcassets/stop.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "stop.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Binary file added Kyapchar/Assets.xcassets/stop.imageset/stop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
357 changes: 85 additions & 272 deletions Kyapchar/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Kyapchar/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.0.1</string>
<string>0.0.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
119 changes: 90 additions & 29 deletions Kyapchar/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,65 @@ import Cocoa
import AVFoundation
import AppKit

class ViewController: NSViewController, AVCaptureFileOutputRecordingDelegate {
class ViewController: NSViewController {
var session: AVCaptureSession!
var output: AVCaptureMovieFileOutput!
var audioRecorder: AVAudioRecorder!
var castedVideoURL = NSURL()
var micAudioURL = NSURL()
var finalVideoURL = NSURL()
var storedFileInfo: [[String]] = []

let micAudioRecordSettings = [AVSampleRateKey : NSNumber(float: Float(44100.0)),
AVFormatIDKey : NSNumber(int: Int32(kAudioFormatMPEG4AAC)),
AVNumberOfChannelsKey : NSNumber(int: 1),
AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.Medium.rawValue))]

@IBOutlet weak var recordButton: NSButton!
@IBOutlet weak var durationLabel: NSTextField!
@IBOutlet weak var debugInfoLabel: NSTextField!
@IBOutlet weak var pauseResumeButton: NSButton!
@IBOutlet weak var storedFileInfoTableView: NSTableView!

@IBAction func onRecordClick(sender: NSButton) {
if (sender.state == NSOffState) {
sender.title = "▶️"
sender.image = NSImage(named: "record")
sender.toolTip = "Start Recording"
pauseResumeButton.hidden = true
stopRecording()
} else {
sender.title = "🔴"
durationLabel.stringValue = ""
sender.image = NSImage(named: "stop")
sender.toolTip = "Stop Recording"
pauseResumeButton.hidden = false
debugInfoLabel.stringValue = ""
startRecording()
}
}


@IBAction func onPauseResumeClick(sender: NSButton) {
if output.recordingPaused {
output.resumeRecording()
audioRecorder.record()
} else {
output.pauseRecording()
audioRecorder.pause()
}
}

override func viewDidLoad() {
super.viewDidLoad()
recordButton.setNextState()
pauseResumeButton.hidden = true
pauseResumeButton.image = NSImage(named: "pause")
pauseResumeButton.toolTip = "Pause Recording"

storedFileInfoTableView.setDataSource(self)
}

func startRecording() {
storedFileInfo = []
session = AVCaptureSession()
output = AVCaptureMovieFileOutput()
(castedVideoURL, micAudioURL, finalVideoURL) = filePaths()
storedFileInfoTableView.reloadData()

let input = AVCaptureScreenInput(displayID: CGMainDisplayID())
let screen = NSScreen.mainScreen()!
Expand Down Expand Up @@ -82,25 +104,9 @@ class ViewController: NSViewController, AVCaptureFileOutputRecordingDelegate {

func stopRecording() {
if output != nil {
output!.stopRecording()
output?.stopRecording()
audioRecorder?.stop()
durationLabel.stringValue = "Please wait..."
}
}

func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
if (error != nil) {
debugPrint(error)
}

dispatch_async(dispatch_get_main_queue()) {
self.session?.stopRunning()

self.generateFinalVideo()

self.session = nil
self.output = nil
self.audioRecorder = nil
debugInfoLabel.stringValue = "Please wait..."
}
}

Expand Down Expand Up @@ -131,7 +137,7 @@ class ViewController: NSViewController, AVCaptureFileOutputRecordingDelegate {
assetExportSession?.outputFileType = "com.apple.quicktime-movie"
assetExportSession?.outputURL = finalVideoURL
assetExportSession?.exportAsynchronouslyWithCompletionHandler({
dispatch_async(dispatch_get_main_queue(), {
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), {
if assetExportSession?.status == AVAssetExportSessionStatus.Completed {
let duration: Int = Int(CMTimeGetSeconds((assetExportSession?.asset.duration)!))
var fileSize : Float = 0.0
Expand All @@ -145,11 +151,12 @@ class ViewController: NSViewController, AVCaptureFileOutputRecordingDelegate {
print("Error while fetching final video file size: \(error)")
}


self.durationLabel.stringValue = "\(self.formatDuration(duration)) Seconds\n📁 \(self.finalVideoURL.absoluteString)\nSize \(String(format: "%.2f", fileSize)) MB"
self.storedFileInfo = [["Duration", "\(self.formatDuration(duration)) Seconds"],
["Location", self.finalVideoURL.absoluteString.stringByReplacingOccurrencesOfString("file://" + NSHomeDirectory(), withString: "~")],
["Size", "\(String(format: "%.2f", fileSize)) MB"]]
} else {
print("Export failed")
self.durationLabel.stringValue = "Export failed!"
self.storedFileInfo = [["Error", "Export Failed"]]
}

do {
Expand All @@ -159,6 +166,10 @@ class ViewController: NSViewController, AVCaptureFileOutputRecordingDelegate {
print("Error while deleting temporary files: \(error)")
}

dispatch_async(dispatch_get_main_queue(), {
self.debugInfoLabel.stringValue = ""
self.storedFileInfoTableView.reloadData()
})
})
})
}
Expand Down Expand Up @@ -187,3 +198,53 @@ class ViewController: NSViewController, AVCaptureFileOutputRecordingDelegate {
return String(format: "%02d:%02d:%02d", hrs, mins, secs)
}
}

extension ViewController: AVCaptureFileOutputRecordingDelegate {
func captureOutput(captureOutput: AVCaptureFileOutput!, didPauseRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) {
pauseResumeButton.image = NSImage(named: "resume")
pauseResumeButton.toolTip = "Resume Recording"
}

func captureOutput(captureOutput: AVCaptureFileOutput!, didResumeRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) {
pauseResumeButton.image = NSImage(named: "pause")
pauseResumeButton.toolTip = "Pause Recording"

NSApp.mainWindow?.miniaturize(self)
}

func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) {
NSApp.mainWindow?.miniaturize(self)
}

func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
if (error != nil) {
debugPrint(error)
}

dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) {
self.session?.stopRunning()

self.generateFinalVideo()

self.session = nil
self.output = nil
self.audioRecorder = nil
}
}
}

extension ViewController: NSTableViewDataSource {
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
return storedFileInfo.count
}

func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn?, row: Int) -> AnyObject? {
let item = storedFileInfo[row]

if tableColumn?.identifier == "key" {
return item[0]
} else {
return item[1]
}
}
}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ Simple screen and microphone audio recorder for Mac

## Preview

Watch this demo screencast captured using Kyapchar - https://youtu.be/B4RfdJCZ6yU.
Watch this demo screencast captured using Kyapchar (v0.0.1) - https://youtu.be/B4RfdJCZ6yU.

![monosnap 2016-10-22 21-22-17](https://cloud.githubusercontent.com/assets/876195/19620567/e80b1d9e-989d-11e6-9757-4868b7fdba96.png)
![kyapchar 2016-10-22 21-21-16](https://cloud.githubusercontent.com/assets/876195/19620566/e80986c8-989d-11e6-85f2-2662aad6bfb6.png)

![Preview](https://monosnap.com/file/zyTid534mXjmJbY7G8GYNPUzMejT64.png)

## Notice

Expand Down

0 comments on commit 690d7f1

Please sign in to comment.