1

我发现了 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
}

但在另一台设备上,我什么也听不到。

请指导可能出现的问题。

4

2 回答 2

1

您没有从字典中检索任何数据,只是缓冲区的数量。

    NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    NSLog(@"%@", dict);

    int numberOfBuffers = [dict[@"buffersCount"] intValue];
    NSLog(@"%d", numberOfBuffers);

    AudioBufferList *audioBufferList = (AudioBufferList *)malloc(sizeof(AudioBufferList));
    if (NULL == audioBufferList) {
        NSLog(@"*** malloc failure for allocating audioBufferList memory");
        return;
    }
    audioBufferList->mNumberBuffers = (int)dict[@"buffersCount"];

    for(NSDictionary *obj in dict[@"buffers"]){
        NSMutableData *data = [NSMutableData dataWithData:obj[@"data"]];
        audioBufferList->mBuffers[0].mNumberChannels = (int)obj[@"channels"];
        audioBufferList->mBuffers[0].mDataByteSize = (int)[data length];
        audioBufferList->mBuffers[0].mData = (AudioBuffer *)malloc([data length]);

        if (NULL == audioBufferList->mBuffers[0].mData) {
            NSLog(@"*** malloc failure for allocating mData memory");
            return;
        }
        memcpy(audioBufferList->mBuffers[0].mData, [data mutableBytes], [data length]);

        NSLog(@"Buffer has %d channels and wants %d bytes of data.", (unsigned int)audioBufferList->mBuffers[0].mNumberChannels, (unsigned int)audioBufferList->mBuffers[0].mDataByteSize);
    }
于 2014-06-12T03:44:01.447 回答
0

有多种方法可以打开两个设备之间的网络连接。您想要执行此操作,并以您希望将数据从设备 A 获取到设备 B 的任何格式简单地打包数据。

从这里开始:http: //nshipster.com/multipeer-connectivity/

于 2014-05-19T22:06:53.653 回答