wav格式音頻合成

參數(shù)介紹

//audioPath1 第一個音頻路徑

//audioPath2第二個音頻路徑

//outputPath合成之后的音頻路徑

+ (void)mixAudio:(NSString *)audioPath1 andAudio:(NSString *)audioPath2 toFile:(NSString *)outputPath

{

NSMutableArray *arrary = [[NSMutableArray alloc]init];

NSURL *url = [NSURL fileURLWithPath:audioPath1];

AVAsset *avset = [AVAsset assetWithURL:url];

NSURL *url1 = [NSURL fileURLWithPath:audioPath2];

AVAsset *avset1 = [AVAsset assetWithURL:url1];

[arrary addObject:avset];

[arrary addObject:avset1];

AVMutableComposition *mainComposition = [[AVMutableComposition alloc] init];

AVMutableCompositionTrack *soundtrackTrack = [mainComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

CMTime insertTime = kCMTimeZero;

for(AVAsset *videoAsset in arrary)

{

[soundtrackTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:insertTime error:nil];

// Updating the insertTime for the next insert

insertTime = CMTimeAdd(insertTime, videoAsset.duration);

}

NSURL *outptVideoUrl = [NSURL fileURLWithPath:outputPath];

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mainComposition presetName:AVAssetExportPresetAppleM4A];

// Setting attributes of the exporter

exporter.outputURL=outptVideoUrl;

exporter.outputFileType =@"com.apple.m4a-audio"; //AVFileTypeQuickTimeMovie;

exporter.shouldOptimizeForNetworkUse = YES;

[exporter exportAsynchronouslyWithCompletionHandler:^{

dispatch_async(dispatch_get_main_queue(), ^{

//completion(exporter);

// [self exportDidFinish:exporter];

// [self exportDidFinish:exporter:assets];

NSLog(@"-----合并完成-----");

[[NSNotificationCenter defaultCenter]postNotificationName:@"success" object:nil];

});

}];

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容