繁体   English   中英

将音频文件从iPhone音频库保存到应用程序文档文件夹时,如何减小音频文件的大小?

[英]How to reduce the size of audio file when I save it from iphone audio library to my application document folder?

我正在制作音频应用。 在其中,我从iPhone设备音频库中选取了一首歌曲,然后将其保存到我的应用程序文档文件夹中。 但是,当我将其保存到我的文档文件夹中时,歌曲的大小将增加4-5倍。 这是我的代码-

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

请帮我做错了什么。

您的程序正在获取一个任意音频文件,并将其转换为您指定的格式。 在此转换->写入步骤中,可能是“解压缩”源文件的音频数据,增加了采样率,增加了通道数或可能还有许多其他事情。 在不知道源音频文件的情况下-可能会引入这种增长。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM