你好 我的最终目标是创建一个UITableViewController带有应用内录制内容的,FileManager以遍历/Documents/目录并列出在其中找到的所有录制内容。
UITableViewController
FileManager
/Documents/
使用以下设置,一次录制就可以正常进行录制和播放:
// In VC#1 func setupRecorder(){ let audioSession:AVAudioSession = AVAudioSession.sharedInstance() do { try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord) } catch { print("Audio Setup Error: \(error)") } do { try audioSession.setActive(true) } catch { print("Audio Setup Error: \(error)") } let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] audioFileName = "test.caf" audioFileUrl = documentsDirectory.appendingPathComponent(audioFileName) print("\n\nrecording url :\(audioFileUrl.absoluteString)\n\n") let recordSettings = [ AVFormatIDKey: Int(kAudioFormatAppleLossless), AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue, AVEncoderBitRateKey : 320000, AVNumberOfChannelsKey: 2, AVSampleRateKey : 44100.0 ] as [String : Any] do { audioRecorder = try AVAudioRecorder(url: audioFileUrl, settings: recordSettings) } catch let error as NSError{ print("Audio Setup Error: \(error)") audioRecorder = nil } audioRecorder.delegate = self audioRecorder.isMeteringEnabled = true audioRecorder.prepareToRecord() } ... override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == GraphViewController.getEntrySegue() { let navController = segue.destination as! UINavigationController let destination = navController.topViewController as! GraphViewController destination.audioFileName = audioFileName destination.audioFileUrl = audioFileUrl } // In VC#2 func setupAudioPlayer() { do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) } catch { print(error) } do { try AVAudioSession.sharedInstance().setActive(true) } catch { print(error) } do { audioPlayer = try AVAudioPlayer(contentsOf: audioFileUrl) } catch { print("Audio Player Setup Error: \(error)") } audioPlayer.prepareToPlay() }
这一切都很好,很花哨。它可以毫无问题地播放录制的音频文件。但是,当我尝试通过FileManager查找文件时,FileManager无法找到它。下面列出了我尝试查找此文件的尝试。
print("\(FileManager.default.fileExists(atPath: audioFileUrl.absoluteString))") // -> False print("\(FileManager.default.contents(atPath: audioFileUrl.absoluteString))") // -> nil
显然,我必须缺少某些东西……尤其是考虑到AVAudioPlayer已成功读取具有完全相同的URL的音频文件。路径显示为:
AVAudioPlayer
file:///var/mobile/Containers/Data/Application/1DA6BD0F-5A23-4228-92A9-A083E55ACE21/Documents/test.caf
有任何想法吗?🧐🤔
非常感谢!
编辑: 在将url作为属性存储之前,我正在执行以下操作,重新计算VC#2中的documentsDirectory,但是也没有成功:
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] let results = try? FileManager.default.contentsOfDirectory(atPath: documentsDirectory.absoluteString)
您无法保存沙盒文件的URL或路径字符串以供以后使用,因为沙盒可以移动。您需要成功完成之前的工作:计算文档目录URL,然后进行检查。您应该完全使用URL,而不要进行任何调用absoluteString。这完全是个错误的电话,让您失望了。
absoluteString
在此示例中,我检查文件目录中是否有任何文件:
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] let results = try? FileManager.default.contentsOfDirectory(at: documentsDirectory, includingPropertiesForKeys: nil)