gpt4 book ai didi

iphone - 如何在 iOS 中连接 2 或 3 个音频文件?

转载 作者:行者123 更新时间:2023-12-03 18:29:44 25 4
gpt4 key购买 nike

我是 Objective-C 的新手,在 iPhone 开发方面只有 5 个月的经验。

我需要什么:
我需要将 2 个或更多音频文件连接成一个,并将结果导出为 aiff、mp3、caf 或 m4a 格式。

例如:
第一个音频文件包含“您需要”,第二个“下载”和第三个“文档”。
每个音频部分都取决于用户的操作。

我花了两天时间,运气不佳。那个地方是我最后的边疆。

我将非常感谢一段代码。

最佳答案

下面的代码可用于合并音频文件。

输入文件:要在数组 audioIds 中提供的文件的 ID。 例如。 audio1.mp3、audio2.mp3 … audioN.mp3 可在文档文件夹中使用输出文件:combined.m4a

     - (BOOL) combineVoices {

NSError *error = nil;
BOOL ok = NO;


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];


CMTime nextClipStartTime = kCMTimeZero;
//Create AVMutableComposition Object.This object will hold our multiple AVMutableCompositionTrack.
AVMutableComposition *composition = [[AVMutableComposition alloc] init];

AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

for (int i = 0; i< [self.audioIds count]; i++) {
int key = [[self.audioIds objectAtIndex:i] intValue];
NSString *audioFileName = [NSString stringWithFormat:@"audio%d", key];

//Build the filename with path
NSString *soundOne = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3", audioFileName]];
//NSLog(@"voice file - %@",soundOne);

NSURL *url = [NSURL fileURLWithPath:soundOne];
AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeAudio];
if ([tracks count] == 0)
return NO;
CMTimeRange timeRangeInAsset = CMTimeRangeMake(kCMTimeZero, [avAsset duration]);
AVAssetTrack *clipAudioTrack = [[avAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
ok = [compositionAudioTrack insertTimeRange:timeRangeInAsset ofTrack:clipAudioTrack atTime:nextClipStartTime error:&error];
if (!ok) {
NSLog(@"Current Video Track Error: %@",error);
}
nextClipStartTime = CMTimeAdd(nextClipStartTime, timeRangeInAsset.duration);
}

// create the export session
// no need for a retain here, the session will be retained by the
// completion handler since it is referenced there
AVAssetExportSession *exportSession = [AVAssetExportSession
exportSessionWithAsset:composition
presetName:AVAssetExportPresetAppleM4A];
if (nil == exportSession) return NO;

NSString *soundOneNew = [documentsDirectory stringByAppendingPathComponent:@"combined.m4a"];
//NSLog(@"Output file path - %@",soundOneNew);

// configure export session output with all our parameters
exportSession.outputURL = [NSURL fileURLWithPath:soundOneNew]; // output path
exportSession.outputFileType = AVFileTypeAppleM4A; // output file type

// perform the export
[exportSession exportAsynchronouslyWithCompletionHandler:^{

if (AVAssetExportSessionStatusCompleted == exportSession.status) {
NSLog(@"AVAssetExportSessionStatusCompleted");
} else if (AVAssetExportSessionStatusFailed == exportSession.status) {
// a failure may happen because of an event out of your control
// for example, an interruption like a phone call comming in
// make sure and handle this case appropriately
NSLog(@"AVAssetExportSessionStatusFailed");
} else {
NSLog(@"Export Session Status: %d", exportSession.status);
}
}];

return YES;
}

关于iphone - 如何在 iOS 中连接 2 或 3 个音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6644046/

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