-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path4C0846DB-B9A0-461F-8869-D3F4789BCE81.codesnippet
More file actions
166 lines (137 loc) · 4.94 KB
/
4C0846DB-B9A0-461F-8869-D3F4789BCE81.codesnippet
File metadata and controls
166 lines (137 loc) · 4.94 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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDECodeSnippetCompletionPrefix</key>
<string>swift audio recorder </string>
<key>IDECodeSnippetCompletionScopes</key>
<array>
<string>TopLevel</string>
</array>
<key>IDECodeSnippetContents</key>
<string>
class CreatingFlashCardTableVC: UITableViewController {
@IBOutlet weak var saveButton: UIButton!
@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var playButton: UIButton!
@IBOutlet weak var stopButton: UIButton!
var audioPlayer: AVAudioPlayer?
var audioRecorder: AVAudioRecorder?
var soundFileUrl = AppDelegate.share.documentsDirectoryURL.appendingPathComponent("sound.mp3")
let audioSession = AVAudioSession.sharedInstance()
var audioButtonState: AudioButtonState?{
didSet {
switch audioButtonState! {
case .origin:
playButton.isEnabled = false
stopButton.isEnabled = false
case .play:
stopButton.isEnabled = true
recordButton.isEnabled = false
case .record:
playButton.isEnabled = false
stopButton.isEnabled = true
case .stop:
stopButton.isEnabled = false
playButton.isEnabled = true
recordButton.isEnabled = true
}
}
}
let recordSettings =
[AVEncoderAudioQualityKey: AVAudioQuality.max.rawValue,
AVEncoderBitRateKey: 16,
AVNumberOfChannelsKey: 2,
AVSampleRateKey: 44100.0] as [String : Any]
override func viewDidLoad() {
super.viewDidLoad()
configAudioRecorder()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension CreatingFlashCardTableVC {
enum AudioButtonState: Int {
case origin
case play
case stop
case record
}
func configAudioRecorder() {
audioButtonState = .origin
do {
try audioSession.setCategory(
AVAudioSessionCategoryPlayAndRecord)
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}
do {
try audioRecorder = AVAudioRecorder(url: soundFileUrl,
settings: recordSettings as [String : AnyObject])
audioRecorder?.prepareToRecord()
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}
}
@IBAction func recordAudio(_ sender: AnyObject) {
if audioRecorder?.isRecording == false {
audioButtonState = .record
audioRecorder?.record()
}
}
@IBAction func stopAudio(_ sender: AnyObject) {
audioButtonState = .stop
if audioRecorder?.isRecording == true {
audioRecorder?.stop()
} else {
audioPlayer?.stop()
}
}
@IBAction func playAudio(_ sender: AnyObject) {
if audioRecorder?.isRecording == false {
audioButtonState = .play
do {
try audioPlayer = AVAudioPlayer(contentsOf:
(audioRecorder?.url)!)
audioPlayer?.delegate = self
audioPlayer?.volume = 1
audioPlayer?.prepareToPlay()
audioPlayer?.play()
} catch let error as NSError {
print("audioPlayer error: \(error.localizedDescription)")
}
}
}
}
// MARK: - AVAudioPlayerDelegate
extension CreatingFlashCardTableVC: AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
audioButtonState = .stop
}
func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, error: Error?) {
print("Audio Play Decode Error")
}
}
// MARK: - AVAudioPlayerDelegate
extension CreatingFlashCardTableVC: AVAudioRecorderDelegate {
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
}
func audioRecorderEncodeErrorDidOccur(_ recorder: AVAudioRecorder, error: Error?) {
print("Audio Record Encode Error")
}
}</string>
<key>IDECodeSnippetIdentifier</key>
<string>4C0846DB-B9A0-461F-8869-D3F4789BCE81</string>
<key>IDECodeSnippetLanguage</key>
<string>Xcode.SourceCodeLanguage.Swift</string>
<key>IDECodeSnippetTitle</key>
<string>Swift 3: Audio Recorder </string>
<key>IDECodeSnippetUserSnippet</key>
<true/>
<key>IDECodeSnippetVersion</key>
<integer>2</integer>
</dict>
</plist>