iphone - 转换音频时读取大尺寸音频文件的内存问题

标签 iphone ios objective-c

我有 sampeaudio.caf(300 MB) 音频文件。我将音频 .caf 转换为 .wav 格式。它说警告“收到内存警告。Level = 1”和“收到内存警告。Level = 2”之后应用程序崩溃了。但它适用于 ipad。如何在转换过程中使用缓冲区读取少量数据。

这是我的代码:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"sampleaudio" ofType:@"caf"];

NSURL *assetURL = [NSURL fileURLWithPath:soundFilePath];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];

NSError *assetError = nil;
AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:songAsset
                                                           error:&assetError]
;
if (assetError) {
    NSLog (@"error: %@", assetError);
    return;
}

AVAssetReaderOutput *assetReaderOutput = [AVAssetReaderAudioMixOutput
                                          assetReaderAudioMixOutputWithAudioTracks:songAsset.tracks
                                          audioSettings: nil];
if (! [assetReader canAddOutput: assetReaderOutput]) {
    NSLog (@"can't add reader output... die!");
    return;
}
[assetReader addOutput: assetReaderOutput];

NSString *strWavFileName = [NSString stringWithFormat:@"%@.wav",[[soundFilePath lastPathComponent] stringByDeletingPathExtension]];
NSString *wavFilePath = [delegate.strCassettePathSide stringByAppendingPathComponent:strWavFileName];

if ([[NSFileManager defaultManager] fileExistsAtPath:wavFilePath])
{
    [[NSFileManager defaultManager] removeItemAtPath:wavFilePath error:nil];
}
NSURL *exportURL = [NSURL fileURLWithPath:wavFilePath];
AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:exportURL
                                                      fileType:AVFileTypeWAVE
                                                         error:&assetError];
if (assetError)
{
    NSLog (@"error: %@", assetError);
    return;
}

AppDelegate *appDelegate =[[UIApplication sharedApplication]delegate];
int nSampleRate=[[appDelegate.dictWAVQuality valueForKey:@"samplerate"] integerValue];
AudioChannelLayout channelLayout;
memset(&channelLayout, 0, sizeof(AudioChannelLayout));
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
                                [NSNumber numberWithFloat:nSampleRate], AVSampleRateKey,
                                [NSNumber numberWithInt:2], AVNumberOfChannelsKey,
                                [NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)], AVChannelLayoutKey,
                                [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
                                [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
                                [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                                [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
                                nil];
AVAssetWriterInput *assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
                                                                          outputSettings:outputSettings];
if ([assetWriter canAddInput:assetWriterInput])
{
    [assetWriter addInput:assetWriterInput];
}
else
{
    NSLog(@"can't add asset writer input... die!");
    return;
}

assetWriterInput.expectsMediaDataInRealTime = NO;

[assetWriter startWriting];
[assetReader startReading];

AVAssetTrack *soundTrack = [songAsset.tracks objectAtIndex:0];
CMTime startTime = CMTimeMake (0, soundTrack.naturalTimeScale);
[assetWriter startSessionAtSourceTime: startTime];

__block UInt64 convertedByteCount = 0;

dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue", NULL);

[assetWriterInput requestMediaDataWhenReadyOnQueue:mediaInputQueue
                                        usingBlock: ^
 {
     while (assetWriterInput.readyForMoreMediaData)
     {
         CMSampleBufferRef nextBuffer = [assetReaderOutput copyNextSampleBuffer];
         if (nextBuffer)
         {
             // append buffer
             [assetWriterInput appendSampleBuffer: nextBuffer];
             convertedByteCount += CMSampleBufferGetTotalSampleSize (nextBuffer);
         }
         else
         {
             [assetWriterInput markAsFinished];
             //              [assetWriter finishWriting];
             [assetReader cancelReading];

             break;
         }
     }
 }];}

如何纠正内存问题。

最佳答案

  CMSampleBufferRef nextBuffer = [assetReaderOutput copyNextSampleBuffer];
  if (nextBuffer)
  {
       // append buffer
       [assetWriterInput appendSampleBuffer: nextBuffer];
       convertedByteCount += CMSampleBufferGetTotalSampleSize (nextBuffer);


     ////////////////////you need to add following line////////////////////

       CMSampleBufferInvalidate(nextBuffer); 
       CFRelease(nextBuffer);
       nextBuffer = NULL;
 }

关于iphone - 转换音频时读取大尺寸音频文件的内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19315854/

相关文章:

ios - 是否可以在iOS模拟器中捕获整个网页的屏幕截图?

ios - dispatch_source_t 处理函数的计时问题——我是否遵循正确的调度计时器模式?

ios - 如果 UITableView 中的字符串/对象为空,如何隐藏标题

iphone - 目标 - C : Loading image from URL?

iphone - iPhone sdk中sqlite中的三角函数

iphone - 如何检查是否结束xml解析

iphone - 我可以连接 UISearchBar 的清除按钮吗?

c++ - 为 iOS 4.x 使用 Boost 和 C++0x 方言

objective-c - 如何在两个字符串中查找公共(public)子字符串

ios - NSMakeRange(i, 1) 是什么意思?