iosaudioaudioqueueservices

how to play silence using AudioQueueEnqueueBuffer... with kAudioFormatMPEG4AAC_HE(VBR)


I am working with a network application, so someting i will lose packet or where i have start play , there is no data for playing. this is my source code:

struct AQPlayerState *pAqData = (struct AQPlayerState *) m_aqData ;
UInt32 numBytesReadFromFile = 0 ;
UInt32 numPackets = 0 ;
char * pdata = (char *)inBuffer->mAudioData ;
for ( int i = 0 ; i < pAqData->mNumPacketsToRead ; ++i )
{
    unsigned int uiRead =GetOneFrame(pdata, pAqData->bufferByteSize - numBytesReadFromFile) ;
    if ( uiRead == 0 )
        break ;

    AudioStreamPacketDescription * packetDescs = pAqData->mPacketDescs + i ;
    packetDescs->mDataByteSize = uiRead ;
    packetDescs->mStartOffset = numBytesReadFromFile ;
    packetDescs->mVariableFramesInPacket = 0 ;
    numBytesReadFromFile += uiRead ;
    pdata += uiRead ;
    ++numPackets ;
}
if (numPackets > 0) 
{
    inBuffer->mAudioDataByteSize = numBytesReadFromFile ;
    OSStatus state = AudioQueueEnqueueBuffer (pAqData->mQueue, inBuffer, (pAqData->mPacketDescs ? numPackets : 0), pAqData->mPacketDescs) ;
    NSLog(@"HandleOutputBuffer packet count:%lu, res:%lu", numPackets, state) ;
}
else 
{
    inBuffer->mAudioDataByteSize = 0 ;
    (*(pAqData->mPacketDescs)).mDataByteSize = 0 ;
    (*(pAqData->mPacketDescs)).mStartOffset = 0 ;
    (*(pAqData->mPacketDescs)).mVariableFramesInPacket = 0 ;
    OSStatus state = AudioQueueEnqueueBuffer (pAqData->mQueue, inBuffer, 0, nil) ;
    NSLog(@"no packet: enqueuebuffer res:%ld", state) ;
}

but if these is no packet, i get a error kAudioQueueErr_BufferEmpty,so i just want to know how to play a silence frame with aac_he type(vbr)


Solution

  • The question is asked by me 2 years ago, I have get a solution actually.

    The solution is that recording a silence packet using recording tools.

    First find a recording tool and set the record parameters to AAC_HE. Then start recording and keep quiet. Save the file.

    When your app launches, load the file in memory and use it when no data to playback.

    As audio queue is a pull-mode api, it is not appropriate if we get data through network. Open AL is a push-mode api but it only supports uncompressed format, so maybe we should decode the compressed data using core-audio then playback using Open AL.