gpt4 book ai didi

ios - 将远程 IO 音频单元从一部 iPhone 流式传输到另一部

转载 作者:行者123 更新时间:2023-11-29 12:45:43 24 4
gpt4 key购买 nike

我找到了 3-4 个显示远程 IO 音频单元用法的示例。喜欢here , here等。他们将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com