0

我一直在尝试按照此处的说明序列化本地音频文件,而不是将数据包直接提供给 AudioQueBuffer,我想使用GKSession通过蓝牙/wifi 发送它。我收到 EXC_BAD_ACCESS 错误:

- (void)broadcastServerMusicWithSession:(GKSession *)session playerName:(NSString *)name clients:    (NSArray *)clients
{
    self.isServer = YES;

    _session = session;
    _session.available = NO;
    _session.delegate = self;
    [_session setDataReceiveHandler:self withContext:nil];

    CFURLRef     fileURL = (__bridge CFURLRef)[[NSBundle mainBundle] URLForResource:@"mozart"                                                                       withExtension:@"mp3"]; // file URL    
    AudioFile *audioFile = [[AudioFile alloc] initWithURL:fileURL];    

    //no we start sending the data over    
    #define MAX_PACKET_COUNT    4096
    SInt64 currentPacketNumber = 0;
    UInt32 numBytes;
    UInt32 numPacketsToRead;
    AudioStreamPacketDescription    packetDescriptions[MAX_PACKET_COUNT];    

    NSInteger bufferByteSize = 4096;
    if (bufferByteSize < audioFile.maxPacketSize) bufferByteSize = audioFile.maxPacketSize; 
    numPacketsToRead = bufferByteSize/audioFile.maxPacketSize;
    UInt32 numPackets = numPacketsToRead;

    BOOL isVBR = ([audioFile audioFormatRef]->mBytesPerPacket == 0) ? YES : NO;

// this should be in a do while (numPackets < numPacketsToRead) loop.. 
// but i took it out just to narrow down the problem

    NSMutableData *myBuffer = [[NSMutableData alloc] initWithCapacity:numPackets * audioFile.maxPacketSize];

    AudioFileReadPackets (
                          audioFile.fileID,
                          NO,
                          &numBytes,
                          isVBR ? packetDescriptions : 0,
                          currentPacketNumber,
                          &numPackets,
                          &myBuffer
                          );

    NSError *error;        
    if (![session sendDataToAllPeers:packetData withDataMode:GKSendDataReliable error:&error]) 
    {
        NSLog(@"Error sending data to clients: %@", error);
    }

我知道是 AudioFileReadPackets 调用破坏了代码,因为如果我注释掉它,它运行良好

我尝试过使用 xcode 4.3 进行Zombies 分析,但它只是崩溃了。我得到的一个有用的调试技巧是在代码崩溃时中断并使用 gdb 控制台输出错误代码。这就是我得到的:

(lldb) po [$eax className] NSException
error: warning: receiver type 'unsigned int' is not 'id' or interface pointer, consider casting it to 'id'
warning: declaration does not declare anything
error: expected ';' after expression
error: 1 errors parsing expression

但我无能为力..有什么帮助吗?

4

1 回答 1

0

原来上面的代码有两个问题:

  1. [audioFile audioFormatRef]->mBytesPerPacket == 0 编译器对此不满意。如果您参考定义 audioFormatRef 的代码,它会返回一个指向格式结构的指针,该结构有一个 mBytesPerPacket 字段。我不知道为什么上面的语法不起作用

  2. 我不应该将 myBuffer 提供给 AudioFileReadPackets 函数(类型 mismach)。我应该这样做:

    NSMutableData *myBuffer = [[NSMutableData alloc] initWithCapacity:numPackets * audioFile.maxPacketSize];
    const void *buffer = [myBuffer bytes];
    AudioFileReadPackets (
                          audioFile.fileID,
                          NO,
                          &numBytes,
                          isVBR ? packetDescriptions : 0,
                          currentPacketNumber,
                          &numPackets,
                          buffer
                          );
    

这也有效

void *buffer = [myBuffer mutablebytes];

阅读这篇文章以解释这两个选项之间的区别

于 2012-08-09T12:51:49.647 回答