iphone - 转换音频时读取大尺寸音频文件的内存问题
<p><p>我有 sampleaudio.caf(300 MB) 音频文件。我将音频 .caf 转换为 .wav 格式。在应用程序崩溃之后,它会显示警告“收到内存警告。级别 = 1”和“收到内存警告。级别 = 2”。但它适用于ipad。如何在转换发生时使用缓冲区读取少量数据。 </p>
<p>这是我的代码:</p>
<pre><code>NSString *soundFilePath = [ pathForResource:@"sampleaudio" ofType:@"caf"];
NSURL *assetURL = ;
AVURLAsset *songAsset = ;
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 (! ) {
NSLog (@"can't add reader output... die!");
return;
}
;
NSString *strWavFileName = stringByDeletingPathExtension]];
NSString *wavFilePath = ;
if ([ fileExistsAtPath:wavFilePath])
{
[ removeItemAtPath:wavFilePath error:nil];
}
NSURL *exportURL = ;
AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:exportURL
fileType:AVFileTypeWAVE
error:&assetError];
if (assetError)
{
NSLog (@"error: %@", assetError);
return;
}
AppDelegate *appDelegate =[delegate];
int nSampleRate=[ integerValue];
AudioChannelLayout channelLayout;
memset(&channelLayout, 0, sizeof(AudioChannelLayout));
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
, AVFormatIDKey,
, AVSampleRateKey,
, AVNumberOfChannelsKey,
, AVChannelLayoutKey,
, AVLinearPCMBitDepthKey,
, AVLinearPCMIsNonInterleaved,
,AVLinearPCMIsFloatKey,
, AVLinearPCMIsBigEndianKey,
nil];
AVAssetWriterInput *assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
outputSettings:outputSettings];
if ()
{
;
}
else
{
NSLog(@"can't add asset writer input... die!");
return;
}
assetWriterInput.expectsMediaDataInRealTime = NO;
;
;
AVAssetTrack *soundTrack = ;
CMTime startTime = CMTimeMake (0, soundTrack.naturalTimeScale);
;
__block UInt64 convertedByteCount = 0;
dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue", NULL);
[assetWriterInput requestMediaDataWhenReadyOnQueue:mediaInputQueue
usingBlock: ^
{
while (assetWriterInput.readyForMoreMediaData)
{
CMSampleBufferRef nextBuffer = ;
if (nextBuffer)
{
// append buffer
;
convertedByteCount += CMSampleBufferGetTotalSampleSize (nextBuffer);
}
else
{
;
// ;
;
break;
}
}
}];}
</code></pre>
<p>如何纠正内存问题。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><pre><code>CMSampleBufferRef nextBuffer = ;
if (nextBuffer)
{
// append buffer
;
convertedByteCount += CMSampleBufferGetTotalSampleSize (nextBuffer);
////////////////////you need to add following line////////////////////
CMSampleBufferInvalidate(nextBuffer);
CFRelease(nextBuffer);
nextBuffer = NULL;
}
</code></pre></p>
<p style="font-size: 20px;">关于iphone - 转换音频时读取大尺寸音频文件的内存问题,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/19315854/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/19315854/
</a>
</p>
页:
[1]