Я пытаюсь создать приложение, которое записывает и сохраняет аудиоклипы так же, как голосовые заметки. Я могу записать одну аудиозапись и воспроизвести ее, но я не уверен, как сохранить несколько и заполнить их UITableView. У меня действительно нет опыта в подобных вещах, поэтому я бы с благодарностью отнесся к ним. Вот мой код:
Main V C:
@IBOutlet weak var startBtn: UIButton!
@IBOutlet weak var stopBtn: UIButton!
@IBOutlet weak var tableView: UITableView!
var recorderView: RecorderViewController!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.allowsSelection = false
let storyboard = UIStoryboard(name: "Main", bundle: nil)
recorderView = storyboard.instantiateViewController(withIdentifier: "RecorderViewController") as! RecorderViewController
recorderView.delegate = self
recorderView.createRecorder()
//recorderView.view.backgroundColor = UIColor.green
recorderView.modalTransitionStyle = .crossDissolve
recorderView.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
}
@IBAction func start() {
self.present(recorderView, animated: true, completion: nil)
recorderView.startRecording()
}
@IBAction func play() {
do {
try recorderView.recording.play()
} catch {
print(error)
}
}
internal func didFinishRecording(_ recorderViewController: RecorderViewController) {
print(recorderView.recording.url)
}
Регистратор V C:
open weak var delegate: RecorderViewDelegate?
var recording: Recording!
var recordDuration = 0
@IBOutlet weak var tapToFinishBtn: UIButton!
@IBOutlet weak var durationLabel: UILabel!
@IBOutlet weak var voiceRecordHUD: VoiceRecordHUD!
// MARK: View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
createRecorder()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
voiceRecordHUD.update(0.0)
voiceRecordHUD.fillColor = UIColor.green
durationLabel.text = ""
}
open func createRecorder() {/*
let alert = UIAlertController(title: "Add Name", message: "Add a unique name for your recording!", preferredStyle: .alert)
let action1 = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
print("CANCELED")
}
let action2 = UIAlertAction(title: "Ok", style: .default) { (action) in
print("COMPLETED")
}
self.present(alert, animated: true)*/
recording = Recording(to: "recording.m4a")
recording.delegate = self
// Optionally, you can prepare the recording in the background to
// make it start recording faster when you hit `record()`.
DispatchQueue.global().async {
// Background thread
do {
try self.recording.prepare()
} catch {
print(error)
}
}
}
open func startRecording() {
recordDuration = 0
do {
try recording.record()
} catch {
print(error)
}
}
@IBAction func stop() {
delegate?.didFinishRecording(self)
dismiss(animated: true, completion: nil)
recordDuration = 0
recording.stop()
voiceRecordHUD.update(0.0)
}
func audioMeterDidUpdate(_ db: Float) {
print("db level: %f", db)
self.recording.recorder?.updateMeters()
let ALPHA = 0.05
let peakPower = pow(10, (ALPHA * Double((self.recording.recorder?.peakPower(forChannel: 0))!)))
var rate: Double = 0.0
if (peakPower <= 0.2) {
rate = 0.2
} else if (peakPower > 0.9) {
rate = 1.0
} else {
rate = peakPower
}
voiceRecordHUD.update(CGFloat(rate))
voiceRecordHUD.fillColor = UIColor.green
recordDuration += 1
durationLabel.text = String(recordDuration)
}