簡體   English   中英

播放流后,音頻隊列無法錄制音頻

[英]Audio queue cannot record audio after playing a stream

我正在使用Mat Gallager的Audio Streamer來播放音頻流。 在程序的另一點(一個不同的控制器),我試圖從設備的麥克風錄制一些東西。

這是我的設置:

void SFIdentificator::startRecord()

{int i,bufferByteSize; UInt32大小;

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 occurred\n");;
}

}

void SFIdentificator::SetupAudioFormat(UInt32 inFormatID)

{memset(&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類,然后再嘗試記錄一些內容,則甚至不會調用Callback。 但是,如果我不首先使用AudioStreamer,一切都會很好。

誰能指出我正確的方向?

回答我自己的問題,正確設置AudioSession對象的init()是一個問題。 每個會話只能執行一次,再執行一次會導致錯誤,我發現該錯誤無法記錄流。 但是,即使第二個初始化失敗(當然第一個成功),我也可以記錄流。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM