-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChatterFileViewController.swift
More file actions
executable file
·212 lines (157 loc) · 7.18 KB
/
ChatterFileViewController.swift
File metadata and controls
executable file
·212 lines (157 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// ChatterFileViewController.swift
// ChatterApp2.0
//
// Created by David Carter on 3/26/15.
// Copyright (c) 2015 Team16. All rights reserved.
//
import UIKit
import AVFoundation
import CoreData
class ChatterFileViewController : UIViewController {
required init(coder aDecoder: NSCoder) {
var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
self.audioURL = NSUUID().UUIDString + ".m4a"
var pathComponents = [baseString, self.audioURL]
var audioNSURL = NSURL.fileURLWithPathComponents(pathComponents)
var session = AVAudioSession.sharedInstance()
session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
var recordSettings: [NSObject : AnyObject] = Dictionary()
recordSettings[AVFormatIDKey] = kAudioFormatMPEG4AAC
recordSettings[AVSampleRateKey] = 44100.0
recordSettings[AVNumberOfChannelsKey] = 2
self.audioRecorder = AVAudioRecorder(URL: audioNSURL, settings: recordSettings, error: nil)
self.audioRecorder.meteringEnabled = true
self.audioRecorder.prepareToRecord()
// super init below
super.init(coder: aDecoder)
}
@IBOutlet weak var chatterTextField: UITextField! // chatter file name text field
@IBOutlet weak var recordButton: UIButton!
var audioRecorder : AVAudioRecorder
var audioURL: String
var previousViewController = MainTableViewController()
override func viewDidLoad() {
super.viewDidLoad()
// LGI
}
// cancel button
@IBAction func cancelTapped(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
// save button
@IBAction func saveTapped(sender: AnyObject) {
var context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!
// create a chatterfile object
var chatterfile = NSEntityDescription.insertNewObjectForEntityForName("ChatterFile", inManagedObjectContext: context) as! ChatterFile
chatterfile.name = chatterTextField.text
chatterfile.url = self.audioURL
// save sound to core data
context.save(nil)
// dismiss this view controller
self.dismissViewControllerAnimated(true, completion: nil)
}
// record button
@IBAction func recordTapped(sender: AnyObject) {
if self.audioRecorder.recording { // if recording
self.audioRecorder.stop()
self.recordButton.setTitle("RECORD", forState: UIControlState.Normal)
}else{ // if not recording
var session = AVAudioSession.sharedInstance()
session.setActive(true, error: nil)
self.audioRecorder.record()
self.recordButton.setTitle("Stop Recording", forState: UIControlState.Normal)
}
}
}
/* Version with keyboard control stuff that won't run //
// ChatterFileViewController.swift
// ChatterApp2.0
//
// Created by David Carter on 3/26/15.
// Copyright (c) 2015 Team16. All rights reserved.
//
import UIKit
import AVFoundation
import CoreData
class ChatterFileViewController : UIViewController {
required init(coder aDecoder: NSCoder) {
var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
self.audioURL = NSUUID().UUIDString + ".m4a"
var pathComponents = [baseString, self.audioURL]
var audioNSURL = NSURL.fileURLWithPathComponents(pathComponents)
var session = AVAudioSession.sharedInstance()
session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
var recordSettings: [NSObject : AnyObject] = Dictionary()
recordSettings[AVFormatIDKey] = kAudioFormatMPEG4AAC
recordSettings[AVSampleRateKey] = 44100.0
recordSettings[AVNumberOfChannelsKey] = 2
self.audioRecorder = AVAudioRecorder(URL: audioNSURL, settings: recordSettings, error: nil)
self.audioRecorder.meteringEnabled = true
self.audioRecorder.prepareToRecord()
// super init below
super.init(coder: aDecoder)
}
@IBOutlet weak var chatterTextField: UITextField! // chatter file name text field
@IBOutlet weak var recordButton: UIButton!
var audioRecorder : AVAudioRecorder
var audioURL: String
var previousViewController = MainTableViewController()
override func viewDidLoad() {
super.viewDidLoad()
// LGI
/* //// Keyboard stuff that was working but now it isn't ///
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}
func keyboardWillShow(sender: NSNotification) {
self.view.frame.origin.y -= 200
}
func keyboardWillHide(sender: NSNotification) {
self.view.frame.origin.y += 200
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
view.endEditing(true)
super.touchesBegan(touches as Set<NSObject>, withEvent: event)
}
*/
// var instanceOfKeyboard: keyboard = keyboard()
/* func keyboardWasShown(notification: NSNotifcation) {
var info = notication.userInfo!
var keyboardFrame: CGRect = (info[UIKeboardFrameEndUserInfoKey] as NSValue).CGRectValue
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.bottomContraint.constant = keboardFrame.size.height; +20
})
}
*/
// cancel button
func cancelTapped(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
// save button
func saveTapped(sender: AnyObject) {
var context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!
// create a chatterfile object
var chatterfile = NSEntityDescription.insertNewObjectForEntityForName("ChatterFile", inManagedObjectContext: context) as! ChatterFile
chatterfile.name = chatterTextField.text
chatterfile.url = self.audioURL
// save sound to core data
context.save(nil)
// dismiss this view controller
self.dismissViewControllerAnimated(true, completion: nil)
}
// record button
func recordTapped(sender: AnyObject) {
if self.audioRecorder.recording { // if recording
self.audioRecorder.stop()
self.recordButton.setTitle("RECORD", forState: UIControlState.Normal)
}else{ // if not recording
var
session = AVAudioSession.sharedInstance()
session.setActive(true, error: nil)
self.audioRecorder.record()
self.recordButton.setTitle("Stop Recording", forState: UIControlState.Normal)
}
}
};
*/