swift - 在 swift 。当我创建两个同名的声音文件时,第二个是否会替换第一个?

标签 swift nsurl avaudiorecorder

我试图快速制作一个录音机,但遇到了这个问题。如果我创建两个同名的声音文件,第一个文件会被第二个文件替换,还是第一个文件仍然存在但我找不到它?

这是我的代码:

func setup_recorder强文本(){

    var recordSettings = [
        AVFormatIDKey: kAudioFormatAppleLossless,
        AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue,
        AVEncoderBitRateKey : 320000,
        AVNumberOfChannelsKey: 2,
        AVSampleRateKey : 44100.0
    ]

    time_display_1 = 0
    timer_1 = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "timer_update_1", userInfo: nil, repeats: true)
    var dir_path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    var doc_dir: AnyObject = dir_path[0]
    var sound_file_path = doc_dir.stringByAppendingPathComponent("1.m4a")
    sound_file_URL_1 = NSURL(fileURLWithPath: sound_file_path)
    let file_manager = NSFileManager.defaultManager()
    if file_manager.fileExistsAtPath(sound_file_path) {
        println("sound exists")
    }        

    var error: NSError?
    recorder_1 = AVAudioRecorder(URL: sound_file_URL_1!, settings: recordSettings, error: &error)
    if let e = error {
        println(e.localizedDescription)
    }
    else {
        recorder_1.meteringEnabled = true
        recorder_1.prepareToRecord()
    }
}

最佳答案

if i create two sound file with the same name, does the first file get replaced by the second

AVAudioRecorder 将用新数据覆盖(即替换)现有文件。为避免这种情况,请先检查该文件是否存在,如果存在则选择一个不同的名称。

关于swift - 在 swift 。当我创建两个同名的声音文件时,第二个是否会替换第一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29151057/

相关文章:

ios - 每次我尝试在调试器控制台中打印内容时,Xcode 7 都会崩溃

JavaScript Socket.IO 到 Swift

ios - NSString 编码在 url 内容上返回 nil

ios - webview.URL?.absoluteString 返回 nil

iPhone:让录音应用程序在后台运行

ios - Swift 数值和 CGFloat(CGPoint、CGRect 等)

swift - 问题取消选择 tableView 单元格 | swift

ios - 调用 API 时应用程序总是崩溃

ios - 如何将音频文件保存在核心数据中并发送到服务器?

iphone - 在iphone的Documents目录下保存一个wav格式的录音