有没有办法用设备输出声音进行iPhone录音?

时间:2012-01-21 16:39:57

标签: iphone objective-c ios audio-recording recording

我正在使用以下代码(audiotoolbox)测试iPhone中的录音, 但在录音时,我听不到iPhone设备的声音。

有没有办法用设备的声音进行iPhone录音(如果可能的话,“使用内核补丁”并不重要)?

我应该使用其他API吗?

#include <AudioToolbox/AudioQueue.h>
#include <AudioToolbox/AudioFile.h>
#include <AudioToolbox/AudioConverter.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/select.h>

#define AUDIO_BUFFERS 3

typedef struct AQCallbackStruct
{
    AudioStreamBasicDescription mDataFormat;
    AudioQueueRef queue;
    AudioQueueBufferRef mBuffers[AUDIO_BUFFERS];
    AudioFileID outputFile;
    unsigned long frameSize;
    long long recPtr;
    int run;
} AQCallbackStruct;

static void AQInputCallback(
                            void                                 *aqr,
                            AudioQueueRef                        inQ,
                            AudioQueueBufferRef                  inQB,
                            const AudioTimeStamp                 *timestamp,
                            unsigned long                        frameSize,
                            const AudioStreamPacketDescription   *mDataFormat)
{
    AQCallbackStruct *aqc = (AQCallbackStruct *) aqr;

    /* Write data to file */
    if (AudioFileWritePackets (aqc->outputFile, false, inQB->mAudioDataByteSize,
                               mDataFormat, aqc->recPtr, &frameSize, inQB->mAudioData) == noErr)
    {
        aqc->recPtr += frameSize;
    }

    /* Don't re-queue the sound buffers if we're supposed to stop recording */
    if (!aqc->run)
        return;

    AudioQueueEnqueueBuffer (aqc->queue, inQB, 0, NULL);
}

int main(int argc, char *argv[]) {
    AQCallbackStruct aqc;
    AudioFileTypeID fileFormat;
    CFURLRef filename;
    struct timeval tv;
    int i;

    if (argc < 3)
    {
        fprintf(stderr, "Syntax: %s [filename.aif] [seconds]", argv[0]);
        exit(EXIT_FAILURE);
    }

    aqc.mDataFormat.mFormatID = kAudioFormatLinearPCM;//kAudioFormatLinearPCM;
    aqc.mDataFormat.mSampleRate = 44100.0;
    aqc.mDataFormat.mChannelsPerFrame = 2;
    aqc.mDataFormat.mBitsPerChannel = 16;
    aqc.mDataFormat.mBytesPerPacket =
    aqc.mDataFormat.mBytesPerFrame =
    aqc.mDataFormat.mChannelsPerFrame * sizeof (short int);
    aqc.mDataFormat.mFramesPerPacket = 1;
    aqc.mDataFormat.mFormatFlags =
    kLinearPCMFormatFlagIsBigEndian
    | kLinearPCMFormatFlagIsSignedInteger
    | kLinearPCMFormatFlagIsPacked;
    aqc.frameSize = 735;

    AudioQueueNewInput (&aqc.mDataFormat, AQInputCallback, &aqc, NULL,
                        kCFRunLoopCommonModes, 0, &aqc.queue);

    /* Create output file */

    fileFormat = kAudioFileAIFFType;//.caf kAudioFileAIFFType;
    filename = CFURLCreateFromFileSystemRepresentation (NULL, (const UInt8*)argv[1], strlen (argv[1]), false);

    AudioFileCreateWithURL (
                            filename,
                            fileFormat,
                            &aqc.mDataFormat,
                            kAudioFileFlags_EraseFile,
                            &aqc.outputFile
                            );

    /* Initialize the recording buffers */

    for (i=0; i<AUDIO_BUFFERS; i++) {
        AudioQueueAllocateBuffer (aqc.queue, aqc.frameSize, &aqc.mBuffers[i]);
        AudioQueueEnqueueBuffer (aqc.queue, aqc.mBuffers[i], 0, NULL);
    }

    aqc.recPtr = 0;
    aqc.run = 1;

    AudioQueueStart (aqc.queue, NULL);

    /* Hang around for a while while the recording takes place */

    tv.tv_sec = atof(argv[2]);
    tv.tv_usec = 0;
    select(0, NULL, NULL, NULL, &tv);

    /* Shut down recording */

    AudioQueueStop (aqc.queue, true);
    aqc.run = 0;

    AudioQueueDispose (aqc.queue, true);
    AudioFileClose (aqc.outputFile);

    exit(EXIT_SUCCESS);
}

1 个答案:

答案 0 :(得分:0)

您的意思是在录制时无法听到您发送到输出的音频吗? 如果是这样,请检查音量是否非常柔和。 从麦克风录音时,默认情况下,iphone会将主扬声器的输出重新路由到耳机,以防止反馈,因为扬声器就在麦克风旁边。这款耳机扬声器更柔软,因为它可以与您的头部一起使用。尝试插入耳机进行检查。 您可以使用AudioSession API(Apple的文档实际上可以使用)来覆盖它。