Question

It appears that my song file that I am trying to grab from the user library is being blown up to 50MB after the code below. However, I know this song to be 7.2MB. I'd like to attach the music file (DRM free) to an email, but 50MB is obviously too big. The code I use is below. Any help would be appreciated :)

-(void)findSong  {
MPMediaQuery *query = [MPMediaQuery songsQuery];
NSArray *songsQuery = [query collections];

//create media item
MPMediaItemCollection *mySong = [songsQuery objectAtIndex:1];
NSLog(@"song name: %@",[[mySong representativeItem]valueForProperty:MPMediaItemPropertyTitle]);

NSURL *assetURL = [[mySong representativeItem]valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];

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

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

NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [dirs objectAtIndex:0];
NSString *exportPath = [[documentsDirectoryPath stringByAppendingPathComponent:EXPORT_NAME] retain];
if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) {
    [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
}
NSURL *exportURL = [NSURL fileURLWithPath:exportPath];
AVAssetWriter *assetWriter = [[AVAssetWriter assetWriterWithURL:exportURL
                                                       fileType:AVFileTypeCoreAudioFormat
                                                          error:&assetError]
                              retain];
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]
                                        retain];
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: ^ 
 {
     // NSLog (@"top of block");
     while (assetWriterInput.readyForMoreMediaData) {
         CMSampleBufferRef nextBuffer = [assetReaderOutput copyNextSampleBuffer];
         if (nextBuffer) {
             // append buffer
             [assetWriterInput appendSampleBuffer: nextBuffer];
             //             NSLog (@"appended a buffer (%d bytes)", 
             //                    CMSampleBufferGetTotalSampleSize (nextBuffer));
             convertedByteCount += CMSampleBufferGetTotalSampleSize (nextBuffer);

         } else {
             // done!
             [assetWriterInput markAsFinished];
             [assetWriter finishWriting];
             [assetReader cancelReading];
             NSDictionary *outputFileAttributes = [[NSFileManager defaultManager]
                                                   attributesOfItemAtPath:exportPath
                                                   error:nil];
             NSLog(@"export path: %@",exportPath);
             NSLog (@"done. file size is %ld",[outputFileAttributes fileSize]);

             NSURL *exportURL = [NSURL fileURLWithPath:exportPath];
             NSData *data = [NSData dataWithContentsOfURL:exportURL];
             data = [ChannelViewController gzipData:data];
             [self performSelectorOnMainThread:@selector(sendMailWithData:)
                                    withObject:data
                                 waitUntilDone:NO];

             // release a lot of stuff
             [assetReader release];
             [assetReaderOutput release];
             [assetWriter release];
             [assetWriterInput release];
             [exportPath release];
             break;
         }
     }

 }];
NSLog (@"bottom of convertTapped:");
}
Was it helpful?

Solution

You're asking the AVAssetWriter to create a decompressed LPCM file. I didn't know AVAssetWriter did conversions for you. Weird.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top