我遇到了将2个.wav文件混合在一起的问题。
我正在使用此网站的代码:http://eigenclass.blogspot.in/2010/12/merging-wav-files-in-objective-c.html
现在我遇到的问题是,代码通常适用于小于10秒的wav文件,任何超过10秒的内容都会导致应用程序崩溃。
由于一些奇怪的原因,即使我在for循环结束时正确地释放了对象(for循环包含超过10万次迭代并且随着记录大小的增加而增加),对象似乎不会被释放并继续记忆。
这是我的for循环:
short iValueWav1Sample;
short iValueWav2Sample;
short iValueWavSampleAverage;
for (int i=0; i<(outputWavDataSize/2); i++)
{
//simulate little endian by flipping the bytes
wav1DataBuffer1 = [[NSData alloc]initWithData:[wav1Data subdataWithRange:NSMakeRange(44 + (i*2), 1)]];
wav2DataBuffer1 = [[NSData alloc]initWithData:[wav2Data subdataWithRange:NSMakeRange(44 + (i*2), 1)]];
wav1DataBuffer2 = [[NSData alloc]initWithData:[wav1Data subdataWithRange:NSMakeRange(44 + (i*2) + 1, 1)]];
wav2DataBuffer2 = [[NSData alloc]initWithData:[wav2Data subdataWithRange:NSMakeRange(44 + (i*2) + 1, 1)]];
littleEndianHexWav1Sample = [[NSMutableData alloc] initWithData:wav1DataBuffer1];
[littleEndianHexWav1Sample appendData:[NSMutableData dataWithData:wav1DataBuffer2]];
littleEndianHexWav2Sample = [[NSMutableData alloc]initWithData:wav2DataBuffer1];
[littleEndianHexWav2Sample appendData:wav2DataBuffer2];
NSString* wav1HexString = [[littleEndianHexWav1Sample description] substringWithRange:NSMakeRange(1, 4)];
NSString* wav2HexString = [[littleEndianHexWav2Sample description] substringWithRange:NSMakeRange(1, 4)];
unsigned wav1Hexint;
unsigned wav2Hexint;
[[NSScanner scannerWithString:wav1HexString] scanHexInt:&wav1Hexint];
[[NSScanner scannerWithString:wav2HexString] scanHexInt:&wav2Hexint];
unsigned wavAverage;
wavAverage = (wav1Hexint+wav2Hexint);
iValueWav1Sample = wav1Hexint;
iValueWav2Sample = wav2Hexint;
iValueWavSampleAverage = wavAverage;
bigEndian = [[NSData alloc ]initWithBytes:&iValueWavSampleAverage length:2];
littleEndian = [[NSMutableData alloc] initWithData:[bigEndian subdataWithRange:NSMakeRange(1, 1)]];
[littleEndian appendData:[bigEndian subdataWithRange:NSMakeRange(0, 1)]];
[headerBuffer appendData:littleEndian];
//Release stuff
[wav1HexString release];
[wav2HexString release];
[wav1DataBuffer1 release];
[wav2DataBuffer1 release];
[wav1DataBuffer2 release];
[wav2DataBuffer2 release];
[littleEndianHexWav1Sample release];
[littleEndianHexWav2Sample release];
[bigEndian release];
[littleEndian release];
wav1DataBuffer1 = nil;
wav2DataBuffer1= nil;
wav1DataBuffer2 = nil;
wav2DataBuffer2 = nil;
littleEndianHexWav1Sample= nil;
littleEndianHexWav2Sample= nil;
bigEndian= nil;
littleEndian= nil;
}
编辑:
@Usman:
我已经使用performselectorinbackground方法在后台运行混合功能。
我缩小了对象wav1Data和wav2Data的泄漏,这两个NSData都是在起始时设置为自动释放对象的。
NSData * wav1Data = [[NSData alloc ]initWithContentsOfFile:soundFile1Path];
NSData * wav2Data = [[NSData alloc]initWithContentsOfFile:soundFile2Path];
现在我正在做的是,在for循环中创建wav1Data和wav2Data,然后在结束时释放它们。
我已经解决了内存泄漏问题,但现在又出现了另一个问题,即for循环的执行速度已经大大减慢。这必须是因为我在每次迭代时初始化文件的内容。
有任何建议或想法吗?
答案 0 :(得分:0)
我在这里看到的是,您的应用程序可能崩溃主要是因为您在UI线程上执行了更大的操作。您需要在非UI /后台线程中调用此操作。以下是如何做到这一点:
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(processWave:)
object:nil;
[queue addOperation:operation];
[operation release];
[queue release];
OR
[NSThread detachNewThreadSelector: @selector(processWave) toTarget:self withObject:NULL];
更不用说您可能需要自己创建processWave()。
如果它仍然崩溃,请告诉我。