AudioQueueNewInput之前不会播放音频服务

时间:2011-09-14 08:47:10

标签: ios

当录音开始像iphone原生备忘录应用程序时,我需要发出哔声。

我使用AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)方法播放短蜂鸣声(声音文件是wav文件)。

在发出哔哔声后,我启动了AudioQueueNewInput(在AudioToolbox框架中定义)来录制音频。

录音工作正常但没有播放短蜂鸣声。

这是我的代码。

typedef struct
{
    AudioStreamBasicDescription  dataFormat;
    AudioQueueRef                queue;
    AudioQueueBufferRef          buffers[NUM_BUFFERS];
    AudioFileID                  audioFile;
    SInt64                       currentPacket;
    bool                         recording;    
} RecordState;

-(void)startRecording
{

CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef fileRef = CFBundleCopyResourceURL(mainBundle, CFSTR("startBeepSound"), CFSTR("wav"), NULL);
    AudioServicesCreateSystemSoundID(fileRef, &startRecordingSound);
AudioServicesPlaySystemSound(startRecordingSound);

RecordState recordState;
recordState->mSampleRate = 12000.0;
    recordState->mFormatID = kAudioFormatLinearPCM;
    recordState->mFramesPerPacket = 1;
    recordState->mChannelsPerFrame = 1;
    recordState->mBytesPerFrame = 2;
    recordState->mBytesPerPacket = 2;
    recordState->mBitsPerChannel = 16;
    recordState->mReserved = 0;
    recordState->mFormatFlags = kLinearPCMFormatFlagIsBigEndian |
    kLinearPCMFormatFlagIsSignedInteger |
    kLinearPCMFormatFlagIsPacked;

    recordState.currentPacket = 0;

    OSStatus status;
    status = AudioQueueNewInput(&recordState.dataFormat,
                                AudioInputCallback,
                                &recordState,
                                CFRunLoopGetCurrent(),
                                kCFRunLoopCommonModes,
                                0,
                                &recordState.queue);

    if(status == 0)
    {
        for(int i = 0; i < NUM_BUFFERS; i++)
        {
            AudioQueueAllocateBuffer(recordState.queue,
                                     16000, &recordState.buffers[i]);
            AudioQueueEnqueueBuffer(recordState.queue,
                                    recordState.buffers[i], 0, NULL);
        }

        status = AudioFileCreateWithURL(fileURL,
                                        kAudioFileAIFFType,
                                        &recordState.dataFormat,
                                        kAudioFileFlags_EraseFile,
                                        &recordState.audioFile);
        if(status == 0)
        {
            recordState.recording = true;        
            status = AudioQueueStart(recordState.queue, NULL);
            if(status == 0)
            {
            }
        }
    }
}

0 个答案:

没有答案