Question

I'm having a problem playing back audio data after appending extra recording to it. Here's the general sequence of events, which don't return any errors:

AudioQueueNewInput       // Start audio queue
AudioFileCreateWithURL   // Create audio file
AudioFileSetProperty     // Set magic cookie

AudioQueueAllocateBuffer // Allocate buffers
AudioQueueEnqueueBuffer  // Prime buffer
AudioQueueStart          // Start the audio queue

// Record some audio in the audio queue callback...
AudioQueueWritePackets
AudioQueueEnqueueBuffer

AudioQueueStop           // Stop the queue
AudioQueueFlush          // Flush remaining buffers in the queue
AudioFileSetProperty     // Set magic cookie again (needed for some formats?)
AudioQueueDispose        // Dispose queue
AudioFileClose           // Close the file

I can start and stop the queue to append recording and it works great. The audio appends perfectly and I can play back as I expect. The problem comes when I re-open the file, and try to append:

AudioQueueNewInput       // Start audio queue
AudioFileOpenURL         // Re-open audio file
AudioFileGetProperty     // Get packet count and resume recording at last packet
AudioFileOptimize        // "Optimize" the file for appending
AudioFileSetProperty     // Set magic cookie

AudioQueueAllocateBuffer // Allocate buffers
AudioQueueEnqueueBuffer  // Prime buffer
AudioQueueStart          // Start the audio queue

// Record some audio in the audio queue callback...
AudioQueueWritePackets
AudioQueueEnqueueBuffer

AudioQueueStop           // Stop the queue
AudioQueueFlush          // Flush remaining buffers in the queue

I take care to resume recording from the last packet of audio, and indeed, data is being appended to the file because I can see the file size grow. However, when I play back the file, I do not hear the appended audio. It plays back the original audio, but stops before I expect to hear the additional audio.

I've tried different combinations moving around AudioFileOptimize and resetting the magic cookie to no avail.

The audio format I'm using is as follows:

static const AudioStreamBasicDescription kMyAudioFormat = {
    .mFormatID = kAudioFormatAppleIMA4,
    .mSampleRate = 44100.0,
    .mChannelsPerFrame = 2,
    .mBitsPerChannel = 0,
    .mBytesPerPacket = 68,
    .mBytesPerFrame = 0,
    .mFramesPerPacket = 64,
    .mFormatFlags = 0
};

What is going on here? Why won't the appended audio play back after I re-open the AudioFileID?

Was it helpful?

Solution

Needed to call AudioFileClose. For more info see: http://lists.apple.com/archives/Coreaudio-api/2011/Oct/msg00014.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top