我发现了3-4个示例,它们显示了远程IO音频单元的用法。例如herehere等。它们会隐藏来自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
}

但是在另一台设备上,我听不到任何声音。

请指导可能有什么问题。

最佳答案

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

    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);
    }

关于ios - 将远程IO音频单元从一台iPhone流传输到另一台,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23736894/

10-12 01:59