2012-10-26 22 views
2

我正在製作音頻應用程序。其中我從iPhone設備音頻庫中選擇一首歌曲,然後將其保存到我的應用程序文檔文件夾中。但是當我將它保存到我的文檔文件夾時,歌曲的大小會增加4-5倍。這裏是我的代碼 -如何從iPhone音頻庫保存到我的應用程序文檔文件夾時減小音頻文件的大小?

- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection { 
NSLog(@"selected song"); 
[self dismissModalViewControllerAnimated:YES]; 
if ([mediaItemCollection count] < 1) { 
    return; 
} 
song = [[mediaItemCollection items] objectAtIndex:0]; 

NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL]; 
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]; 

NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSLog(@"dirs%@",dirs); 
NSString *documentsDirectoryPath = [dirs objectAtIndex:0]; 
NSLog(@"document dir path%@",documentsDirectoryPath); 
NSString *exportPath = [documentsDirectoryPath stringByAppendingPathComponent:EXPORT_NAME]; 
NSLog(@"export path%@",exportPath); 
if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) { 
    [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil]; 
} 
NSURL *exportURL = [NSURL fileURLWithPath:exportPath]; 
NSLog(@"EXport URL%@",exportURL); 
AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:exportURL 
                 fileType:AVFileTypeCoreAudioFormat 
                 error:&assetError]; 
if (assetError) { 
    NSLog (@"error: %@", assetError); 
    return; 
} 
AudioChannelLayout channelLayout; 
memset(&channelLayout, 0, sizeof(AudioChannelLayout)); 
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo; 
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey, 
           [NSNumber numberWithFloat:44100.0], 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) { 
      [assetWriterInput appendSampleBuffer: nextBuffer]; 
      convertedByteCount += CMSampleBufferGetTotalSampleSize (nextBuffer); 
      NSNumber *convertedByteCountNumber = [NSNumber numberWithLong:convertedByteCount]; 
     } else { 
      [assetWriterInput markAsFinished]; 
      [assetWriter finishWriting]; 
      [assetReader cancelReading]; 
      NSDictionary *outputFileAttributes = [[NSFileManager defaultManager] 
                attributesOfItemAtPath:exportPath 
                error:nil]; 
      NSLog (@"done. file size is %ld", 
        [outputFileAttributes fileSize]); 
      break; 
     } 
    } 

}]; 
NSLog (@"bottom of convertTapped:"); 
} 

請幫我我做錯了什麼。

+5

您正在讀取壓縮音頻並將其解壓縮爲未壓縮格式 - 尺寸增加並不奇怪。 –

+0

你能告訴我我在哪裏做錯了嗎? – Saurabh

+0

這應該是顯而易見的 - 如果您不希望大小增加,那麼您需要以壓縮格式保存文件。 –

回答

0

您的程序正在採取任意音頻文件並將其轉換爲您指定的格式。它可能是'解壓縮'源文件的音頻數據,增加採樣率,增加通道數量,或者在這個轉換 - 寫入步驟期間潛在的許多其他事情。不知道源音頻文件 - 這可能是引入增長的原因。

相關問題