0

我已将 inBuffer->mAudioData 转换为来自网络的 NSData。我使用音频队列回调来做到这一点。

如何转换此 NSData 以便我可以创建一个 .caf 声音文件或直接将输出提供给另一侧的扬声器?

感谢帮助。

编辑 1: 以下是我在发送方用于在 wifi 网络上发送数据的代码:

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*NUM_BUFFERS]);
[[NSNotificationCenter defaultCenter] postNotificationName:@"Recording" object:[NSData dataWithBytes:inBuffer->mAudioData length:inBuffer->mAudioDataByteSize*NUM_BUFFERS]];

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

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

在这里,我将 inBuffer->mAudioData 转换为 NSData,然后发送到 outputStream。

在接收方的另一端,我使用了以下代码:

-(void)audioMessageData:(NSData *)audioData fromUser:(NSString *)userName {
 NSLog(@"DATA = %@",audioData);
}

每当我获得数据时,都会调用上述函数,并获得从发送方 iPhone 发送的 NSData。现在我想播放我收到的任何音频数据。

谢谢您的帮助。

4

1 回答 1

0

最简单的方法是配置音频队列或 RemoteIO 音频单元以播放相同格式的数据(原始线性 PCM?)并开始从无锁循环缓冲区中提取样本(播放静默以应对下溢)。然后只需将接收到的数据从 NSData 网络数据包中复制到循环缓冲区中。

于 2013-09-12T11:23:22.763 回答