1

我在将 2 个 .wav 文件混合在一起时遇到问题。

我正在使用此站点的代码: http: //eigenclass.blogspot.in/2010/12/merging-wav-files-in-objective-c.html

现在我面临的问题是,该代码通常适用于小于 10 秒的 wav 文件,而任何超过 10 秒的文件都会导致应用程序崩溃。

出于某种奇怪的原因,即使我在 for 循环结束时正确释放了对象(for 循环由超过 100 万次迭代组成,并且随着记录大小的增加而增加),这些对象似乎也没有被释放并继续占用记忆。

这是我的 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;

}

编辑:

@乌斯曼:

我已经使用 performselectorinbackground 方法在后台运行混合功能。

我已经将泄漏范围缩小到对象 wav1Data 和 wav2Data,这两个 NSData 在开始时都被设置为自动释放对象。

NSData * wav1Data = [[NSData alloc ]initWithContentsOfFile:soundFile1Path];
NSData * wav2Data = [[NSData alloc]initWithContentsOfFile:soundFile2Path];

现在我正在做的是,在 for 循环中创建 wav1Data 和 wav2Data,然后在最后释放它们。

我已经解决了内存泄漏,但现在又出现了另一个问题,其中 for 循环的执行速度大大减慢。这一定是因为我在每次迭代时都会初始化文件的内容。

有什么建议或想法吗?

4

1 回答 1

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];

或者

[NSThread detachNewThreadSelector: @selector(processWave) toTarget:self withObject:NULL];

更不用说您可能需要自己创建 processWave() 。

如果它仍然崩溃,请告诉我。

于 2012-07-10T08:25:01.347 回答