2

I am making an audio app. In it I have pick a song from iphone device audio library then I have save it to my app document folder. But when I have saved it to my document folder then the size of song increase up to 4-5 times. Here is my code -

 - (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:");
}

Please help me what I have done wrong.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
Saurabh
  • 423
  • 4
  • 11
  • 22
  • 5
    You're reading compressed audio and writing it out as uncompressed - it's not too surprising that the size increases. – Paul R Oct 26 '12 at 09:51
  • Can you suggest me where I am doing wrong? – Saurabh Oct 26 '12 at 09:57
  • This should be obvious - if you don't want the size to increase then you'll need to save the file in a compressed format. – Paul R Oct 26 '12 at 09:59

1 Answers1

0

Your program is taking an arbitrary audio file and converting it to the format you have specified. It may be 'decompressing' the source file's audio data, increasing the sample rate, increasing the channel count, or potentially many other things during this conversion->write step. Without knowing the source audio files -- That's likely where the growth is introduced.

justin
  • 104,054
  • 14
  • 179
  • 226