我发现了 3-4 个示例,这些示例显示了远程 IO 音频单元的用法。像这里,这里等。他们从 iPhone 麦克风中转换模拟音频数据并将它们数字化,然后播放它们。到目前为止一切正常,现在我对音频单元有了很好的了解。
现在,我不想在同一设备上播放存储在 AudioBufferList 中的录制音频,而是将其流式传输到另一台设备。
下面是我如何将 AudioBufferList 转换为 nsdata 并将其发送到另一台设备。
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:@(bufferList->mNumberBuffers) forKey:@"buffersCount"];
NSMutableArray *buffers = [NSMutableArray array];
for ( int i=0; i< bufferList->mNumberBuffers; i++ ) {
NSData *data = [NSData dataWithBytes:bufferList->mBuffers[i].mData length:bufferList->mBuffers[i].mDataByteSize];
NSDictionary *obj = @{@"data":data, @"channels":@(bufferList->mBuffers[i].mNumberChannels)};
[buffers addObject:obj];
}
[dict setValue:buffers forKey:@"buffers"];
NSData *packet = [NSKeyedArchiver archivedDataWithRootObject:dict];
下面是我如何将 nsdata 转换回接收设备上的 AudioBufferList 并将其复制到 tempBuffer 然后播放。
NSDictionary *dict = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:voiceData];
int numberOfBuffers = [dict[@"buffersCount"] intValue];
AudioBufferList *audio = malloc(sizeof(AudioBufferList) + (numberOfBuffers-1)*sizeof(AudioBuffer));
if ( !audio ) {
return;
}
for (int i=0; i < audio->mNumberBuffers; i++) { // in practice we will only ever have 1 buffer, since audio format is mono
AudioBuffer buffer = audio->mBuffers[i];
NSLog(@"Buffer %d has %d channels and wants %d bytes of data.", i, (unsigned int)buffer.mNumberChannels, buffer.mDataByteSize);
// copy temporary buffer data to output buffer
UInt32 size = min(buffer.mDataByteSize, [iosAudio tempBuffer].mDataByteSize); // dont copy more data then we have, or then fits
memcpy(buffer.mData, [iosAudio tempBuffer].mData, size);
buffer.mDataByteSize = size; // indicate how much data we wrote in the buffer
}
但在另一台设备上,我什么也听不到。
请指导可能出现的问题。