2012-08-09 59 views
0

我一直試圖按照說明here序列化本地音頻文件,而不是直接將數據包送到AudioQueBuffer,我想通過藍牙/無線使用GKSession發送它。我得到一個EXC_BAD_ACCESS錯誤:EXC_BAD_ACCESS with AudioFileReadPackets

- (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,但它只是崩潰..一個有用的調試tip我雖然被破壞時的代碼崩潰,用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 

但我不能用它做很多..任何幫助?

+0

堆棧跟蹤之間的區別?任何人? – 2012-08-09 10:15:28

+0

@stackmonster堆棧跟蹤沒有幫助 – abbood 2012-08-09 10:36:28

回答

0

原來有兩個問題與上面的代碼:

  1. [AUDIOFILE audioFormatRef] - > mBytesPerPacket == 0 編譯器不開心這一個..如果你指的是code它定義了audioFormatRef,它返回一個指向格式結構的指針,它有一個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 
            ); 
    

這個工程太

void *buffer = [myBuffer mutablebytes]; 

this後解釋這兩個選項