forked from MartiSK/Team16EME185
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatterFileViewController.swift
executable file
·87 lines (70 loc) · 3.37 KB
/
ChatterFileViewController.swift
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
//
// 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 {
@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
}
required init(coder aDecoder: NSCoder) {
var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
//self.audioURL = NSUUID().UUIDString + ".m4a" // instead of recording in .m4a we're going to record in .mp3 so we can feed it to audiokit
self.audioURL = NSUUID().UUIDString + ".mp3"
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 // instead of recording in .m4a we're going to record in .mp3 so we can feed it to audiokit
recordSettings[AVFormatIDKey] = kAudioFormatMPEGLayer3
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)
}
// 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)
// try converting the .m4a/.mp3 to audiokit
[AKMP3FileInput initWithFilename: chatterfile.name];
// 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)
}
}
}