FileManager找不到音频文件 [英] FileManager cannot find audio file

查看:167
本文介绍了FileManager找不到音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Helloo!我的最终目标是使用 FileManager 创建一个 UITableViewController ,并使用 FileManager 来遍历 / Documents / 目录并列出那里找到的所有录音。

Helloo! My ultimate goal is to create a UITableViewController with recordings made in-app, using FileManager to traverse the /Documents/ directory and list all the recordings found there.

录制和播放功能正常,只需一次录制,以下设置:

The recording and playback are functioning just fine with one recording, with the following setup:

// 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找不到它。下面列出的是我尝试查找此文件。

This is working all fine and dandy; it plays the recorded audio file with no problems whatsoever. However, when I try to find the file via FileManager, FileManager cannot find it. Listed below are my attempts to find this file.

        print("\(FileManager.default.fileExists(atPath: audioFileUrl.absoluteString))") // -> False

        print("\(FileManager.default.contents(atPath: audioFileUrl.absoluteString))") // -> nil

显然我必须遗漏一些东西...特别是给定 AVAudioPlayer 正在使用完全相同的URL成功读取音频文件。路径打印为:

Clearly I must be missing something... especially given AVAudioPlayer is successfully reading the audio file with the exact same url. The path prints as:

file:///var/mobile/Containers/Data/Application/1DA6BD0F-5A23-4228-92A9-A083E55ACE21/Documents/test.caf

这篇关于FileManager找不到音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆