我一直在尝试按照此处的说明序列化本地音频文件,而不是将数据包直接提供给 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
但我无能为力..有什么帮助吗?