0

Apple's Memos Recorder app can overwrite previously recorded audio. During your recording, you can PAUSE and move back on audio and record on it again. I want to clone this feature.

I create an AVAudioEngine and install a TAP on the output of mainMixer and write the PCMBuffer down to a file. I can save file in different linear PCM Formats. The problem is when I want to set AudioFile.framePosition it calls the EXTAudioFileSeek() function. This function as Apple's documentation mentioned: "This function’s behavior with files open for writing is undefined.". I then get follwoing error:

EXCEPTION (-66568): "seek to frame in audio file"

It means I can not use this function to seek the position on AudioFile that created for writing. (I accept it)

I want to know do we have another function in Core Audio that help me to write from a position on a AudioFile?

I follow @RomanTsopin structure and it works properly.

var engine = AVAudioEngine()
var file: AVAudioFile?
var player = AVAudioPlayerNode() // if you need play record later

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    file = AVAudioFile(forWriting: URLFor("my_file.caf")!, settings: engine.inputNode.inputFormatForBus(0).settings, error: nil)
    engine.attachNode(player)
    engine.connect(player, to: engine.mainMixerNode, format: engine.mainMixerNode.outputFormatForBus(0)) //configure graph
    engine.startAndReturnError(nil)
}

@IBAction func record(sender: AnyObject) {
    engine.inputNode.installTapOnBus(0, bufferSize: 1024, format: engine.mainMixerNode.outputFormatForBus(0)) { (buffer, time) -> Void in
        self.file?.writeFromBuffer(buffer, error: nil)
        return
    }
}

@IBAction func stop(sender: AnyObject) {
    engine.inputNode.removeTapOnBus(0)
}


func URLFor(filename: String) -> NSURL? {
    if let dirs : [String] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String] {
        let dir = dirs[0] //documents directory
        let path = dir.stringByAppendingPathComponent(filename)
        return NSURL(fileURLWithPath: path)
    }
    return nil
}

After pause the recording, I want to change the position of writing pointer and write from that position into file.

I am going to try AudioFileWritePackets because it gets inStartingPacket (The packet index for the placement of the first provided packet.)

What do you think? Any idea can help, thanks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Esmaeil
  • 558
  • 5
  • 11
  • You’ll need to use [Audio File Services](https://developer.apple.com/documentation/audiotoolbox/audio_file_services) to seek and write rather than append. It’s the most powerful of the audio file APIs, but not the most fun. – dave234 May 30 '19 at 17:20
  • Thanks @dave234 I have another question can I use AVAudioEngine and Audio File Services at the same time? – Esmaeil May 30 '19 at 19:21

0 Answers0