Record audio on one device and play on another device using UDP

639 views Asked by At

I want to record audio from one iOS device and send and play it to another device at the same time.

I have tried to use AVCaptureAudioDataOutput and send/received CMSampleBufferRef properly on both end but when I play it on receiver device, I am getting : Error Domain=NSOSStatusError Domain Code=1954115647.

I am using following code to convert received CMSampleBufferRef to NSData on Sender Device :

if (captureOutput == audioOutput) {
        AudioBufferList audioBufferList;
        NSMutableData *data=[[NSMutableData alloc] init];
        CMBlockBufferRef blockBuffer;
        CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, NULL, &audioBufferList, 
                                                            sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);

        for( int y=0; y<audioBufferList.mNumberBuffers; y++ )
        {
             AudioBuffer audioBuffer = audioBufferList.mBuffers[y];
             Float32 *frame = (Float32*)audioBuffer.mData;
            [data appendBytes:frame length:audioBuffer.mDataByteSize];
        }
        [self performSelectorOnMainThread:@selector(sendAudio:) withObject:data waitUntilDone:NO];
        CFRelease(blockBuffer);
        blockBuffer=NULL;
}

and following code to play audio on Receiver Device :

audioPlayer = [[AVAudioPlayer alloc] initWithData:audioData error:&error];
audioPlayer.numberOfLoops = -1;
audioPlayer.volume = 1.0;
if (audioPlayer == nil)
    NSLog(@"%@", [error description]);
else
    [audioPlayer play];

It seems that it does not recognize the bytes received as proper audio format.

Please help me to receive this issue.

Thanks.

0

There are 0 answers