我无法播放录制的音频。
我有TableViewController来管理静态TableView,其中一个静态单元格包含一个嵌入式动态表视图,其Delegate和DataSource被分配给另一个类。
我的动态TableView有一个自定义单元格,列出了录音的标题,并有一个按钮,我想按下来播放录制的音频。
表格视图似乎列出了我认为正在工作的录音,因为我正在打印它们的持续时间,但是当我按下播放按钮时,似乎没有发生任何音频或听到任何错误。
我认为在尝试录制音频时,在不同的背景下已经发生了这种情况:
我在这里缺少什么?
感谢。
// MARK:录音机
var recController = RecordingsController()
var audioRec: AudioRecording!
var recorder: AVAudioRecorder!
var session: AVAudioSession!
var currentDateTime: String!
let minute : Double = 60.0
var recordingInProgress: Bool = false
@IBOutlet weak var recordingsTable: UITableView!
@IBOutlet weak var recPlayInProgressLabel: UILabel!
@IBOutlet weak var btnStartStopRecordingOutlet: UIButton! // Starts / Stops Recordings or Playbacks Button Outlet
@IBAction func btnStartStopRecordingAction(sender: UIButton) {
// Starts / Stops voice recording
self.toggleAudioRecorder()
if(self.recordingInProgress){
// Inits Recording Process
self.audioRec = AudioRecording()
let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
self.currentDateTime = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .FullStyle, timeStyle: .ShortStyle)
self.audioRec.name = self.currentDateTime + ".wav"
var pathArray = [dirPath, self.audioRec.name]
self.audioRec.filePath = NSURL.fileURLWithPathComponents(pathArray)
// Setup audio session
self.session = AVAudioSession.sharedInstance()
self.session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
// Initialize and prepare the recorder
self.recorder = AVAudioRecorder(URL: self.audioRec.filePath, settings: nil, error: nil)
self.recorder.delegate = self
self.recorder.prepareToRecord()
self.recorder.record()
}else{
// Sets the Recording Title and stops the Recording Process
(self.recorder.currentTime < self.minute) ? (self.audioRec.title = "[" + (NSString(format:"%.2f", (self.recorder.currentTime)) as String) + "s] " + self.currentDateTime) : (self.audioRec.title = "[" + (NSString(format:"%.2f", (self.recorder.currentTime) / self.minute) as String) + "m] " + self.currentDateTime)
self.recorder.stop()
var audioSession = AVAudioSession.sharedInstance()
audioSession.setActive(false, error: nil)
}
}
func audioRecorderDidFinishRecording(recorder: AVAudioRecorder!, successfully flag: Bool) {
// Save recorded audio
var recordedAudio = (title: recorder.url.lastPathComponent, filePathUrl: recorder.url)
println(recordedAudio.filePathUrl)
self.recController.recordings += [self.audioRec]
self.recordingsTable.reloadData()
}
func toggleAudioRecorder() {
// Toogle audio recording
if(self.recPlayInProgressLabel.text == "playing"){
var alert = UIAlertController(title: "Information", message: "Playback in Progress!\nPlease stop the current playback before starting an audio recording.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}else{
self.recordingInProgress = !self.recordingInProgress
if(self.recordingInProgress){
self.btnStartStopRecordingOutlet.setImage(UIImage(named: "StopIcon"), forState: UIControlState.Normal)
self.recPlayInProgressLabel.text = "recording"
}else{
self.btnStartStopRecordingOutlet.setImage(UIImage(named: "AudioRecIcon"), forState: UIControlState.Normal)
self.recPlayInProgressLabel.text = ""
}
}
}