我正在编写一个应用程序,让乐队学生录制他们的练习课程并能够回放。我不是最有经验的开发人员,但正在努力实现目标。选择单元格后尝试播放我录制的内容时,出现此错误操作无法完成。 (OSStatus 错误 2003334207。)在控制台中。预先感谢您的帮助!这是我的代码:
我只是需要帮助来弄清楚为什么我会收到这个错误。
导入AVFoundation 导入 UIKit
ViewController 类:UIViewController、AVAudioRecorderDelegate、UITableViewDelegate、UITableViewDataSource {
var recordingSession: AVAudioSession!
var audioRecorder: AVAudioRecorder!
var audioPlayer: AVAudioPlayer!
var numberOfRecorders: Int = 0
@IBOutlet weak var myTableView: UITableView!
@IBOutlet weak var startRecordingButton: UIButton!
@IBAction func startRecording(_ sender: UIButton) {
//Check if we have an active recorder
if audioRecorder == nil {
numberOfRecorders += 1
let fileName = getDirectory().appendingPathComponent("\(numberOfRecorders).m4a")
let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 1200,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue]
//Start Recording
do {
audioRecorder = try AVAudioRecorder(url: fileName, settings: settings)
audioRecorder.delegate = self
audioRecorder.record()
startRecordingButton.setTitle("Stop Recording", for: .normal)
} catch {
displayAlert(title: "Oop", message: "Recording Failed")
}
} else {
//Stopping audio recording
audioRecorder.stop()
audioRecorder = nil
UserDefaults.standard.set(numberOfRecorders, forKey: "myNumber")
myTableView.reloadData()
startRecordingButton.setTitle("Start Recording", for: .normal)
}
}
override func viewDidLoad() {
super.viewDidLoad()
recordingSession = AVAudioSession.sharedInstance()
if let number: Int = UserDefaults.standard.object(forKey: "myNumber") as? Int {
numberOfRecorders = number
}
AVAudioSession.sharedInstance().requestRecordPermission() {
[weak self] isGranted in
guard let strongSelf = self else {return}
guard isGranted else {
let settingURL = URL(string: UIApplication.openSettingsURLString)!
UIApplication.shared.open(settingURL, options: [:], completionHandler: nil)
return
}
}
}
//Function that gets path to directory
func getDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentDirectory = paths[0]
return documentDirectory
}
//Function that displays an alert
func displayAlert(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "dismiss", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
//MARK: - Tableview Setup
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfRecorders
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = String(indexPath.row + 1)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let path = getDirectory().appendingPathComponent("\(indexPath.row + 1).m4a")
do {
// Set up the AVAudioSession configuration
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
// Keep an instance of AVAudioPlayer at the UIViewController level
self.audioPlayer = try AVAudioPlayer(contentsOf: getDirectory())
audioPlayer.play()
} catch let error {
print(error.localizedDescription)
}
}
}
我试图更改我的代码以得到相同的错误或类似的错误。无论错误编号如何,我遇到的常见问题是无法播放音频。我正在尝试将它保存到文档目录并将其下载并保存到“开始录制”按钮下方的表格视图中。