AVAssetReader制作MP3文件更大?

时间:2011-06-16 20:42:01

标签: ios iphone avassetwriter

在下面的代码之后,我试图从用户库中获取的歌曲文件似乎被炸掉了50MB。不过,我知道这首歌是7.2MB。我想将音乐文件(DRM免费)附加到电子邮件中,但50MB显然太大了。我使用的代码如下。任何帮助将不胜感激:)

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

1 个答案:

答案 0 :(得分:2)

您要求AVAssetWriter创建解压缩的LPCM文件。我不知道AVAssetWriter为你做了转换。怪异。