[英]EXC_BAD_ACCESS with AudioFileReadPackets
我一直在嘗試按照此處的說明對本地音頻文件進行序列化,只是想將數據包直接饋送到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
但我不能做太多..任何幫助嗎?
原來上面的代碼有兩個問題:
[audioFile audioFormatRef]-> mBytesPerPacket == 0編譯器對此不滿意。如果您引用定義audioFormatRef的代碼 ,它將返回一個指向具有mBytesPerPacket字段的格式結構的指針。上面的語法不起作用
我不應該提供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];
閱讀這篇文章以解釋這兩種選擇之間的區別
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.