gpt4 book ai didi

iphone - 在 iPhone 上合并两个 .caf 文件

转载 作者:太空狗 更新时间:2023-10-30 03:49:39 69 4
gpt4 key购买 nike

我找了又找答案,但似乎找不到。很多人问,但没有人得到答案。我有一个使用 AVAudioRecorder 录制音频的应用程序。现在我只想将两个或多个录音合并到一个可以通过电子邮件发送的文件中。有没有人知道如何做到这一点?

(This answer 建议使用称为音频服务队列的东西,但我对此一无所知)

最佳答案

这并不像您想象的那么简单。我用了AVFoundation framework完全按照您的要求创建 iAmRingtones .它需要从音频文件创建 AVAssets 并设置 AVExportSession。最终结果很棒,但确实需要一些工作。下面大致介绍了我们如何在我们的应用程序中创建导出功能:

- (void) setUpAndAddAudioAtPath:(NSURL*)assetURL toComposition:(AVMutableComposition *)composition {

AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];

AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *sourceAudioTrack = [[songAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

NSError *error = nil;
BOOL ok = NO;

CMTime startTime = CMTimeMakeWithSeconds(0, 1);
CMTime trackDuration = songAsset.duration;
CMTime longestTime = CMTimeMake(848896, 44100); //(19.24 seconds)
CMTimeRange tRange = CMTimeRangeMake(startTime, trackDuration);

//Set Volume
AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
[trackMix setVolume:0.8f atTime:startTime];
[audioMixParams addObject:trackMix];

//Insert audio into track
ok = [track insertTimeRange:tRange ofTrack:sourceAudioTrack atTime:CMTimeMake(0, 44100) error:&error];
}

上述方法从以下方法被调用两次(每个音轨一次):

- (void) exportAudio {

AVMutableComposition *composition = [AVMutableComposition composition];
audioMixParams = [[NSMutableArray alloc] initWithObjects:nil];

//Add Audio Tracks to Composition
NSString *URLPath1 = pathToYourAudioFile1;
NSURL *assetURL1 = [NSURL fileURLWithPath:URLPath1];
[self setUpAndAddAudioAtPath:assetURL1 toComposition:composition];

NSString *URLPath2 = pathToYourAudioFile2;
NSURL *assetURL2 = [NSURL fileURLWithPath:URLPath2];
[self setUpAndAddAudioAtPath:assetURL2 toComposition:composition];

AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
audioMix.inputParameters = [NSArray arrayWithArray:audioMixParams];

//If you need to query what formats you can export to, here's a way to find out
NSLog (@"compatible presets for songAsset: %@",
[AVAssetExportSession exportPresetsCompatibleWithAsset:composition]);

AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
initWithAsset: composition
presetName: AVAssetExportPresetAppleM4A];
exporter.audioMix = audioMix;
exporter.outputFileType = @"com.apple.m4a-audio";
NSString *fileName = @"someFilename";
NSString *exportFile = [[util getDocumentsDirectory] stringByAppendingFormat: @"/%@.m4a", fileName];

// set up export
myDeleteFile(exportFile);
NSURL *exportURL = [NSURL fileURLWithPath:exportFile];
exporter.outputURL = exportURL;

// do the export
[exporter exportAsynchronouslyWithCompletionHandler:^{
int exportStatus = exporter.status;
switch (exportStatus) {
case AVAssetExportSessionStatusFailed:
NSError *exportError = exporter.error;
NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
break;

case AVAssetExportSessionStatusCompleted: NSLog (@"AVAssetExportSessionStatusCompleted"); break;
case AVAssetExportSessionStatusUnknown: NSLog (@"AVAssetExportSessionStatusUnknown"); break;
case AVAssetExportSessionStatusExporting: NSLog (@"AVAssetExportSessionStatusExporting"); break;
case AVAssetExportSessionStatusCancelled: NSLog (@"AVAssetExportSessionStatusCancelled"); break;
case AVAssetExportSessionStatusWaiting: NSLog (@"AVAssetExportSessionStatusWaiting"); break;
default: NSLog (@"didn't get export status"); break;
}
}];

// start up the export progress bar
progressView.hidden = NO;
progressView.progress = 0.0;
[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector (updateExportProgress:)
userInfo:exporter
repeats:YES];

}

关于iphone - 在 iPhone 上合并两个 .caf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7656512/

69 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com