-
Notifications
You must be signed in to change notification settings - Fork 60
/
CustomMediaRecorder.swift
79 lines (68 loc) · 2.47 KB
/
CustomMediaRecorder.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
import Foundation
import AVFoundation
class CustomMediaRecorder {
private var recordingSession: AVAudioSession!
private var audioRecorder: AVAudioRecorder!
private var audioFilePath: URL!
private var originalRecordingSessionCategory: AVAudioSession.Category!
private var status = CurrentRecordingStatus.NONE
private let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
private func getDirectoryToSaveAudioFile() -> URL {
return URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
}
public func startRecording() -> Bool {
do {
recordingSession = AVAudioSession.sharedInstance()
originalRecordingSessionCategory = recordingSession.category
try recordingSession.setCategory(AVAudioSession.Category.playAndRecord)
try recordingSession.setActive(true)
audioFilePath = getDirectoryToSaveAudioFile().appendingPathComponent("\(UUID().uuidString).aac")
audioRecorder = try AVAudioRecorder(url: audioFilePath, settings: settings)
audioRecorder.record()
status = CurrentRecordingStatus.RECORDING
return true
} catch {
return false
}
}
public func stopRecording() {
do {
audioRecorder.stop()
try recordingSession.setActive(false)
try recordingSession.setCategory(originalRecordingSessionCategory)
originalRecordingSessionCategory = nil
audioRecorder = nil
recordingSession = nil
status = CurrentRecordingStatus.NONE
} catch {}
}
public func getOutputFile() -> URL {
return audioFilePath
}
public func pauseRecording() -> Bool {
if(status == CurrentRecordingStatus.RECORDING) {
audioRecorder.pause()
status = CurrentRecordingStatus.PAUSED
return true
} else {
return false
}
}
public func resumeRecording() -> Bool {
if(status == CurrentRecordingStatus.PAUSED) {
audioRecorder.record()
status = CurrentRecordingStatus.RECORDING
return true
} else {
return false
}
}
public func getCurrentStatus() -> CurrentRecordingStatus {
return status
}
}