加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 即使文件存在,AVAudioFile.length也为0

发布时间:2020-12-14 04:55:53 所属栏目:百科 来源:网络整理
导读:我正在创建一个AVAudioFile,用于将声音写入声音文件.如果文件已经存在,我想将framePosition移动到文件的末尾,以便在最后继续写入,而不是替换现有文件. 我做了一些测试,尝试将文件中的缓冲区读入一个具有不同URL的新文件,以便它不会覆盖原始文件.当我尝试将缓
我正在创建一个AVAudioFile,用于将声音写入声音文件.如果文件已经存在,我想将framePosition移动到文件的末尾,以便在最后继续写入,而不是替换现有文件.

我做了一些测试,尝试将文件中的缓冲区读入一个具有不同URL的新文件,以便它不会覆盖原始文件.当我尝试将缓冲区读入新文件时,我遇到了崩溃:

let audioFile = try AVAudioFile(forReading: [URL to existing .caf file])
let audioFrameCount = AVAudioFrameCount(UInt32(audioFile.length))
let audioBuffer = AVAudioPCMBuffer(PCMFormat: audioFile.processingFormat,frameCapacity: audioFrameCount)

let newAudioFile = try AVAudioFile(forWriting: [another URL],settings: self.engine.mainMixerNode.outputFormatForBus(0).settings)
try newAudioFile.readIntoBuffer(audioBuffer,frameCount: audioFrameCount!) <-- CRASHES ON THIS LINE

崩溃日志:因未捕获的异常’com.apple.coreaudio.avfaudio’而终止应用程序,原因:’error -50′

伙计,我真的很讨厌CoreAudio崩溃日志.他们绝对没有告诉我!

是不是可以将数据读入为写入而创建的文件中?

UPDATE

好的,所以经过一些建议我做了一些改变.基本上,这些是我正在采取的步骤:

>检查文件是否已存在.
>如果是,请将其打开以进行读取并获取音频缓冲区.
>创建一个用于写入的新文件(使用相同的文件URL)
>使用writeFromBuffer将缓冲区从旧文件写入新文件
>将新文件的framePosition移动到最后,以便我可以继续写入/录制它.

但是,写入后,新文件的长度为0.

这是我的代码:

//Check if a file already exists. If so continue to record at the end of it
var audioBuffer : AVAudioPCMBuffer!
var audioFrameCount : AVAudioFrameCount!
if (NSFileManager.defaultManager().fileExistsAtPath(self.audioRecordURL.path!)) {
    do {
        let existingAudioFile = try AVAudioFile(forReading: self.audioRecordURL)
        audioFrameCount = AVAudioFrameCount(existingAudioFile.length)
        if (audioFrameCount > 0) {
            audioBuffer = AVAudioPCMBuffer(PCMFormat: existingAudioFile.processingFormat,frameCapacity: audioFrameCount)
        }
    } catch let error as NSError {
        NSLog("Error reading buffer from file %@",error.localizedDescription)
    }
}

//Create a new file. This will replace the old file
do {
    self.audioFile = try AVAudioFile(forWriting: self.audioRecordURL,settings: self.engine.mainMixerNode.outputFormatForBus(0).settings)
} catch let error as NSError {
    NSLog("Error creating AVAudioFile %@",error.localizedDescription)
}

//Read the audio buffer from the old file into the new file
if (audioBuffer != nil) {
    do {
        try self.audioFile.writeFromBuffer(audioBuffer)
        self.audioFile.framePosition = self.audioFile.length
    } catch let error as NSError {
        NSLog("Error reading buffer into file %@",error.localizedDescription)
    }
}

顺便说一句,readIntoBuffer的命名对我来说非常混乱.听起来好像你应该使用该方法将文件读入缓冲区,但根据文档,您应该使用它来将缓冲区读入文件中?那么为什么我不能使用该方法将缓冲区添加到我的文件中呢?为什么我必须使用writeFromBuffer?

更新2

所以,我设法解决了它.显然我必须先调用readIntoBuffer来实际用数据填充缓冲区才能使用它.所以我添加了这一行

try existingAudioFile.readIntoBuffer(audioBuffer)

audioBuffer = AVAudioPCMBuffer(PCMFormat: existingAudioFile.processingFormat,frameCapacity: audioFrameCount)

解决方法

不确定这只是你在这里提供的代码中的一个拼写错误,而且我不是这方面的专家,但可能你的意思是让崩溃的行:

try audioFile.readIntoBuffer(audioBuffer,frameCount: audioFrameCount!)

因为你无法从打开写入的文件中读取(newAudioFile).

然后在填充该缓冲区后,您需要使用writeFromBuffer写入新文件
https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioFile_Class/#//apple_ref/occ/instm/AVAudioFile/writeFromBuffer:error:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读