我一直在尝试按照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
但我不能做太多..任何帮助吗?
最佳答案
原来上述代码有两个问题:
如果您引用定义了audioFormatRef的code,它会返回一个指向具有mBytesPerPacket字段的格式结构的指针。.我不知道为什么上面的语法虽然
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帖子以解释这两个选项之间的区别