Load NSData from AudioBufferList into AVAudioPlayer

1k views Asked by At

From a delegate method I am receiving an AudioBufferList while I am recording audio. I am trying to collect the data from the AudioBufferList and save it so I can load it into my AVAudioPlayer but the AVAudioPlayer throws an error and I am unable to play the recording. I need to be able to play the audio through AVAudioPlayer without having the file, just by using the AudioBufferList.

Originally I was saving the recording to a file then loading it into AVAudioPlayer but with this method I was unable to append to the recording without having to make another audio file then merging the 2 files after the append was made. This was taking to much time and I would still like to be able to listen to the recording between appends. So now I am not saving the audio file so that I can keep appending to it until I wish to save it. The problem with this is the NSData that I am saving from the AudioBufferList is not loading into the AVAudioPlayer properly.

Here is my code for gathering the NSData:

- (void)      microphone:(EZMicrophone *)microphone
       hasBufferList:(AudioBufferList *)bufferList
      withBufferSize:(UInt32)bufferSize
withNumberOfChannels:(UInt32)numberOfChannels
{
    AudioBuffer sourceBuffer = bufferList->mBuffers[0];

    if (audioBuffer.mDataByteSize != sourceBuffer.mDataByteSize)
    {
        free(audioBuffer.mData);
        audioBuffer.mDataByteSize = sourceBuffer.mDataByteSize;
        audioBuffer.mData = malloc(sourceBuffer.mDataByteSize);
    }

    int currentBuffer =0;
    int maxBuf = 800;

    for( int y=0; y< bufferList->mNumberBuffers; y++ )
    {
        if (currentBuffer < maxBuf)
        {
            AudioBuffer audioBuff = bufferList->mBuffers[y];
            Float32 *frame = (Float32*)audioBuff.mData;

            [data appendBytes:frame length:audioBuffer.mDataByteSize];
            currentBuffer += audioBuff.mDataByteSize;
        }
        else
        {
            break;
        }
    }
}

When I try and load the NSData into AVAudioPlayer I get the following error:

self.audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:&err];

err: Error Domain=NSOSStatusErrorDomain Code=1954115647 "The operation couldn’t be completed. (OSStatus error 1954115647.)"

Any help would be appreciated.

Thank you,

0

There are 0 answers