Record audio in NSData

I have established a TCP connection between two iPhones, and I can send NSData packets between them. I would like to talk into a microphone and get a recording as an NSData object and send it to another iPhone. I have successfully used Audio Queue Services for audio recording and playback, but I have not been able to get the recording as NSData. I posted a question about converting a record to NSData when using Audio Queue Services , but it has not received me yet.

So I would like to hear if there is any other approach that I can take to talk into the iPhone microphone and enter it as raw data?

Update:

I need to send packets while recording. For instance. every second during recording, I send data recorded during that second.

+4
source share
3 answers

Both audio queues and the RemoteIO Audio Unit will give you real-time raw sound buffers with fairly low latency. You can take the buffer pointer and byte length specified in each sound callback to create a new NSData block. RemoteIO provides minimal latency, but may require that network messaging is performed outside the callback flow.

+5
source

Using AVAudioRecorderas follows:

NSURL *filePath = //Your desired path for the file
NSDictionary *settings; //The settings for the recorded file
NSError *error = nil;
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:filePath settings:recordSetting error:&error];
....
[recorder record];
....
[recorder stop];
....

And extract NSDatafrom file:

NSData *audioData = [[NSData alloc] initWithContentsOfFile:filePath];

AVAudioRecorder, .

Edit:

, subdataWithRange: NSData. , . NSTimer , . , .

0

, iphone:

void AudioInputCallback(void * inUserData,
                    AudioQueueRef inAQ,
                    AudioQueueBufferRef inBuffer,
                    const AudioTimeStamp * inStartTime,
                    UInt32 inNumberPacketDescriptions,
                    const AudioStreamPacketDescription * inPacketDescs)
{
RecordState * recordState = (RecordState*)inUserData;
if (!recordState->recording)
{
    printf("Not recording, returning\n");
}

// if (inNumberPacketDescriptions == 0 && recordState->dataFormat.mBytesPerPacket != 0)
// {
//     inNumberPacketDescriptions = inBuffer->mAudioDataByteSize / recordState->dataFormat.mBytesPerPacket;
// }

printf("Writing buffer %lld\n", recordState->currentPacket);

OSStatus status = AudioFileWritePackets(recordState->audioFile,
                                        false,
                                        inBuffer->mAudioDataByteSize,
                                        inPacketDescs,
                                        recordState->currentPacket,
                                        &inNumberPacketDescriptions,
                                        inBuffer->mAudioData);
NSLog(@"DATA = %@",[NSData dataWithBytes:inBuffer->mAudioData length:inBuffer->mAudioDataByteSize]);

[[NSNotificationCenter defaultCenter] postNotificationName:@"Recording" object:[NSData dataWithBytes:inBuffer->mAudioData length:inBuffer->mAudioDataByteSize]];

if (status == 0)
{
    recordState->currentPacket += inNumberPacketDescriptions;
}

AudioQueueEnqueueBuffer(recordState->queue, inBuffer, 0, NULL);
}

, iPhone .

, . , . , .

0

All Articles