-
Notifications
You must be signed in to change notification settings - Fork 3
/
SoundsRecordingController.swift
114 lines (85 loc) · 3.48 KB
/
SoundsRecordingController.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
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
//
// SoundsRecordingController.swift
// Audio Editor
//
// Created by David Ilenwabor on 5/18/18.
// Copyright © 2018 DJTech. All rights reserved.
//
import UIKit
import AVFoundation
class SoundsRecordingController: UIViewController, AVAudioRecorderDelegate {
var audioRecorder : AVAudioRecorder!
@IBOutlet weak var recordingLabel: UILabel!
@IBOutlet weak var recordingButton: UIButton!
@IBOutlet weak var stopLabel: UILabel!
@IBOutlet weak var stopRecordingButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
stopRecordingButton.isEnabled = false //disables stop recording button
stopLabel.isHidden = true //Hide Stop Label
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
@IBAction func recordAudio(_ sender: AnyObject) {
stopRecordingButton.isEnabled = true //enables stopRecording button
stopLabel.isHidden = false
recordingButton.isEnabled = false
recordingLabel.text = "Recording in Progress"
setUpAnimation()
initializeRecorder()
}
@IBAction func stopRecording(_ sender: UIButton ) {
stopRecordingButton.isEnabled = false
stopLabel.isHidden = true
recordingButton.isEnabled = true
recordingLabel.text = "Tap to Record"
audioRecorder.stop()
let session = AVAudioSession.sharedInstance()
try! session.setActive(false)
}
func setUpAnimation(){
self.recordingButton.alpha = 0.0
UIView.animate(withDuration: 2.0, animations: {
self.recordingButton.alpha = 0.0
}) { (finished) in
UIView.animate(withDuration: 2.0, animations: {
self.recordingButton.alpha = 1.0
//fade in button
})
}
}
func initializeRecorder() {
let directory = NSSearchPathForDirectoriesInDomains(.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] as String
let recordingName = "myRecording.wav"
let pathArray = [directory, recordingName]
let filePath = URL(string: pathArray.joined(separator: "/"))//filepath is an optional URL
print(filePath!)
let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: .defaultToSpeaker)
try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.record()
}
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
if flag {
performSegue(withIdentifier: "recordingFinished", sender: audioRecorder.url)
}
else {
print("Recording failed")
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "recordingFinished"{
let soundPlayingVC = segue.destination as! SoundsPlayingController
let recordedUrl = sender as! URL
soundPlayingVC.recordedAudioURL = recordedUrl
}
}
}