【问题标题】:How to get list of audio which I had stored in documents directory如何获取我存储在文档目录中的音频列表
【发布时间】:2016-12-30 03:05:51
【问题描述】:

我将音频文件存储在我的文档目录中。

这是在文档目录中存储音频的代码

func getDocumentsDirectory() -> NSURL {

    let paths =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    return paths
}

func getRecordingsURL() -> NSURL {

    let date = NSDate()
    let fileURL = getDocumentsDirectory().URLByAppendingPathComponent("\(date).m4a")
    let audioURL = NSURL(fileURLWithPath: fileURL.path!)
    return audioURL
}
@IBAction func btnRecordPressed(sender: AnyObject) {

    let settings = [
        AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
        AVSampleRateKey: 12000.0,
        AVNumberOfChannelsKey: 1 as NSNumber,
        AVEncoderAudioQualityKey: AVAudioQuality.High.rawValue
    ]

    do {
        audioRecorder = try AVAudioRecorder(URL: getRecordingsURL(), settings: settings)
        audioRecorder!.delegate = self
        audioRecorder!.record()


}

所以现在我想要这个完整的音频文件列表,那么我怎样才能得到它。我想在uitableview中显示。

【问题讨论】:

    标签: ios swift uitableview nsfilemanager nsdocumentdirectory


    【解决方案1】:

    以下函数查找 DocumentsDirectory 中具有给定扩展名的所有文件并返回文件路径数组。

    func findFilesWith(extension: String) -> [AnyObject]
    {
        var matches = [AnyObject]()
        let fileManager = NSFileManager.defaultManager()
    
        let files = fileManager.enumeratorAtPath(NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
        // *** this section here adds all files with the chosen extension to an array ***
        for item in files!
        {
            let fileURL = item as! NSURL
    
            if (fileURL.pathExtension == extension)
            {
                matches.append(fileURL)
            }
        }
        return matches
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-07
      • 1970-01-01
      • 2015-09-19
      • 1970-01-01
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      相关资源
      最近更新 更多