美文网首页
wav格式音频合成

wav格式音频合成

作者: 绛紫哟 | 来源:发表于2017-06-27 17:59 被阅读61次

    参数介绍

    //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];
    
    });
    
    }];
    
    }
    

    相关文章

      网友评论

          本文标题:wav格式音频合成

          本文链接:https://www.haomeiwen.com/subject/cwiecxtx.html