音频队列在播放流后无法录制音频



我正在使用Mat Gallager的音频流媒体来播放音频流。在程序的另一点(一个不同的控制器),我正在尝试从设备的麦克风中记录一些内容。

这是我的设置:

void SFIdentificator::startRecord()

{ int i,bufferBytesize; uint32 size;

try {
    numberOfPackets = 0;
    // specify the recording format
    SetupAudioFormat(kAudioFormatLinearPCM);
    AudioQueueNewInput(  &mRecordFormat,
                       MyInputBufferHandler,
                       this /* userData */,
                       CFRunLoopGetMain() /* run loop */, kCFRunLoopCommonModes /* run loop mode */,
                       0 /* flags */, &mQueue);
    mRecordPacket = 0;
    size = sizeof(mRecordFormat);
    AudioQueueGetProperty(mQueue, kAudioQueueProperty_StreamDescription, &mRecordFormat, &size);
    bufferByteSize = ComputeRecordBufferSize(&mRecordFormat, kBufferDurationSeconds);   // enough bytes for half a second

    size = sizeof(mRecordFormat);
    XThrowIfError(AudioQueueGetProperty(mQueue, kAudioQueueProperty_StreamDescription,
                                        &mRecordFormat, &size), "couldn't get queue's format");
    for (i = 0; i < kNumberRecordBuffers; ++i) {
        XThrowIfError(AudioQueueAllocateBuffer(mQueue, bufferByteSize, &mBuffers[i]), "AudioQueueAllocateBuffer failed");
        XThrowIfError(AudioQueueEnqueueBuffer(mQueue, mBuffers[i], 0, NULL), "AudioQueueEnqueueBuffer failed");
    }
    mIsRunning = true;
    XThrowIfError(AudioQueueStart(mQueue, NULL), "AudioQueueStart failed");

} catch (CAXException e) {
    char buf[256];
    fprintf(stderr, "Error: %s (%s)n", e.mOperation, e.FormatError(buf));
}catch (...) {
    fprintf(stderr, "An unknown error occurredn");;
}

}

void SFIdentificator::SetupAudioFormat(UInt32 inFormatID)

{ memset(&amp; mrecordformat,0,sizeof(mrecordformat));

UInt32 size = sizeof(mRecordFormat.mSampleRate);
XThrowIfError(AudioSessionGetProperty(  kAudioSessionProperty_CurrentHardwareSampleRate, &size, &mRecordFormat.mSampleRate), "couldn't get hardware sample rate");
size = sizeof(mRecordFormat.mChannelsPerFrame);
XThrowIfError(AudioSessionGetProperty(  kAudioSessionProperty_CurrentHardwareInputNumberChannels, &size, &mRecordFormat.mChannelsPerFrame), "couldn't get input channel count");
mRecordFormat.mFormatID = inFormatID;
if (inFormatID == kAudioFormatLinearPCM){
    // if we want pcm, default to signed 16-bit little-endian
    mRecordFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
    //      mRecordFormat.mBitsPerChannel = 16;

    mRecordFormat.mBytesPerPacket = mRecordFormat.mBytesPerFrame = (mRecordFormat.mBitsPerChannel / 8) * mRecordFormat.mChannelsPerFrame;
    mRecordFormat.mFramesPerPacket = 1;
    mRecordFormat.mFormatID         = kAudioFormatLinearPCM;
    mRecordFormat.mSampleRate       = 32000.0;
    mRecordFormat.mChannelsPerFrame = 1;
    mRecordFormat.mBitsPerChannel   = 16;
    mRecordFormat.mBytesPerPacket   =  mRecordFormat.mBytesPerFrame = mRecordFormat.mChannelsPerFrame * sizeof (SInt16);
    mRecordFormat.mFramesPerPacket  = 1;
}

}

UInt32 SFIdentificator::ComputeRecordBufferSize(const AudioStreamBasicDescription *format, float seconds){
static const int maxBufferSize = 0x50000;
int maxPacketSize = format->mBytesPerPacket;
if (maxPacketSize == 0) {
    UInt32 maxVBRPacketSize = sizeof(maxPacketSize);
    AudioQueueGetProperty (mQueue, kAudioQueueProperty_MaximumOutputPacketSize, &maxPacketSize, &maxVBRPacketSize);
}
Float64 numBytesForTime = DataFormat().mSampleRate * maxPacketSize * seconds;
//    *outBufferSize = (UInt32)(numBytesForTime < maxBufferSize ? numBytesForTime : maxBufferSize);
return (UInt32)(numBytesForTime < maxBufferSize ? numBytesForTime : maxBufferSize);

}

看来,如果我首先使用audiostreamer类并尝试以后录制一些内容,甚至连回调都没有被调用。但是,如果我不首先使用audiostreamer,那么一切都很好。

有人可以指向正确的方向吗?

回答我自己的问题,这是正确设置AudioSession对象的INIT()的问题。每个会话只能完成一次,然后再进行一次会导致错误,我已经确定无法记录流。但是,即使第二个初始化失败了,我也可以记录流(当然是成功的)。

相关内容

  • 没有找到相关文章