ios - 带有AudioFileReadPackets的EXC_BAD_ACCESS

标签 ios audio serialization audioqueueservices

我一直在尝试按照here指令序列化本地音频文件,而不仅仅是将数据包直接馈送到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 profiling,但是它只是崩溃..当代码崩溃并使用gdb控制台输出错误代码时,我得到的一个有用的调试tip中断了。这就是我得到的:
(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

但我不能做太多..任何帮助吗?

最佳答案

原来上述代码有两个问题:

  • [audioFile audioFormatRef]-> mBytesPerPacket == 0
    如果您引用定义了audioFormatRef的code,它会返回一个指向具有mBytesPerPacket字段的格式结构的指针。.我不知道为什么上面的语法虽然
  • 不起作用
  • 我不应该为AudioFileReadPackets函数(类型mistmach)提供myBuffer。我应该做这样的事情:
    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];
    

    阅读this帖子以解释这两个选项之间的区别

    关于ios - 带有AudioFileReadPackets的EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11880587/

    相关文章:

    ios - 如何更改 iCarousel 库的包装属性?

    objective-c - 我可以仅使用 Interface Builder 将 nib 内容加载到 UIView 中吗?

    ios - 在 installTapOnBus () 方法中我不能做什么?

    django - DRF CreateAPIView 中的条件语句

    ios - 下载图像和视网膜显示

    ios - 有没有什么方法可以在没有应用程序登录的情况下跟踪应用程序购买中的用户

    javascript - 我的JavaScript声音无法在Windows XP中播放?

    javascript - 我可以在Flask中创建指向页面上特定项目的自定义链接吗?

    Python Marshmallow 和 PyCharm 类型提示

    序列化异常 : type not included in serializable type set