带有AudioFileReadPackets的EXC_BAD_ACCESS

时间:2012-08-09 09:42:02

标签: ios audio serialization audioqueueservices

我一直在尝试按照here指令序列化本地音频文件,而不是直接将数据包送到AudioQueBuffer,我希望使用{{3}通过蓝牙/ 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进行GKSession,但它只是崩溃了......一个有用的调试Zombies profiling当代码崩溃并使用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

但我不能做很多事情......任何帮助?

1 个答案:

答案 0 :(得分:0)

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

  1. [audioFile audioFormatRef] - &gt; mBytesPerPacket == 0 编译器对这个问题不满意..如果你引用定义audioFormatRef的code,它会返回一个指向格式struct的指针,它有一个mBytesPerPacket字段..我不知道为什么上面的语法不是虽然工作

  2. 我不应该将myBuffer提供给AudioFileReadPackets函数(输入mistmach)..而是我应该这样做:

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

    void *buffer = [myBuffer mutablebytes];
    

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