FileExistAtPath返回false

时间:2015-10-01 06:09:08

标签: objective-c nsfilemanager avaudiorecorder

好的我用AVAudioRecorder录制了一个音频文件。当我完成录制委托方法调用时..

- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag{
       filePathAudio = [NSString stringWithFormat:@"%@",avrecorder.url];
       BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:[avrecorder.url path]];
}

当我调试并检查filePathAudio的值时,它显示

file:///private/var/mobile/Containers/Bundle/Application/E0B11F18-4607-402D-AE2C-B032F40E0ADF/MyApp.app/recordTest.WAV

和[avrecoder.url path]返回

/private/var/mobile/Containers/Bundle/Application/E0B11F18-4607-402D-AE2C-B032F40E0ADF/MyApp.app/recordTest.WAV

当我检查' fileExist'它显示错误。

有人可以帮助我,我想检查我记录的文件大小..

2 个答案:

答案 0 :(得分:1)

使用此:

BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:[avrecorder.url path]];

答案 1 :(得分:1)

在Swift 4中,您可以使用FileManager的fileExist函数检查URL路径下文件的存在,如下所示。

var fileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
fileURL?.appendPathComponent("file.txt")
let doesFileExist = FileManager.default.fileExists(atPath: fileURL?.path ?? "non-existent-file")

为了方便起见,此处将其包装在AVAudioRecorderDelegate的函数中以匹配您的代码。

import AVFoundation

class FancyViewController: UIViewController, AVAudioRecorderDelegate {

        func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
            let doesFileExist = FileManager.default.fileExists(atPath: recorder.url.path)
            print("The file at \(recorder.url.path) does \(doesFileExist ? "" : "not") exist.")
        }
 ...