美文网首页iOS 开发每天分享优质文章iOS开发
多个音频的拼接(不是音频的混拼)

多个音频的拼接(不是音频的混拼)

作者: 91阿生 | 来源:发表于2017-03-29 16:23 被阅读405次

<前言>:

开始做音频拼接的时候, 一头雾水, 谷歌百度各种查, 但是都是千篇一律的混拼,把demo下载下来,运行看了下基本都是混拼(两个音频同时播放, 而不是A音频播完后接着播B音频). 混拼其实和拼接的代码差不多, 只是有些参数需要改d动, 因此, 接下去我把混拼和拼接的代码都写出来, 供大家分享, 顺便也记录下.
创建一个继承NSObject的类, 这里我用

@interface FileManagerTool : NSObject
@end

@implementation FileManagerTool
@end

混拼:

在 FileManagerTool.h 中提供一个方法:

参数介绍:  
  1. mp3Paths: 存放着你录制后每个音频文件(.caf格式)转为MP3格式后音频文件的本地地址(我这里是用MP3格式音频做的拼接,不知道其他格式成不成)
  2. 要把最后拼接完成的音频存放的文件地址
  3. 拼接完成后的block回调
- (void)pieceMP3File:(NSArray *)mp3Paths 
   savePath:(NSString *)filePath 
   finished:(void(^)(NSString *reslutPath, BOOL isPieceSuccess))finished;

在 FileManagerTool.m 中提供实现方法:
- (void)pieceMP3File:(NSArray *)mp3Paths
savePath:(NSString *)filePath
finished:(void(^)(NSString *reslutPath, BOOL isPieceSuccess))finished {
BOOL isPieceSuccess;

       // 0.一个合成的工程文件
       AVMutableComposition *composition = [AVMutableComposition composition];

       for (NSInteger i = 0; i<self.audioes.count; i++) {
            NSString *auidoPath = [[NSBundle mainBundle] pathForResource:[self.audioes objectAtIndex:i] ofType:nil];
            // 根据每个音频本地地址创建音频资源
            AVURLAsset *audioAsset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:auidoPath]];
            // 创建工程文件中的轨道,有音频轨、视频轨等,里面可以插入各种对应的素材
            AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:0];
            // 获取音频素材轨道
            AVAssetTrack *audioAssetTrack = [[audioAsset tracksWithMediaType:AVMediaTypeAudio] firstObject];
            // 工程文件插入音频素材轨道
            [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:audioAssetTrack atTime:kCMTimeZero error:&error];
            if (error) {
                  NSLog(@"%@", error);
                  isPieceSuccess = NO;
            } else {
                  isPieceSuccess = YES;
            }
        }

            // 合并后的文件导出 - 音频文件目前只找到合成m4a类型的
            AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetAppleM4A];
            NSString *outPutFilePath = [[filePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"resultAudio.m4a"];
            // 查看当前session支持的fileType类型
            NSLog(@"---%@",[session supportedFileTypes]);
            session.outputURL = [NSURL fileURLWithPath:outPutFilePath];
            session.outputFileType = AVFileTypeAppleM4A;
            [session exportAsynchronouslyWithCompletionHandler:^{
                 NSLog(@"合并完成----%@", outPutFilePath);
                 if (finished) {
                     finished(outPutFilePath, isPieceSuccess);
                 }
         }];
   }; 

拼接:

注意:拼接只用到一个AVMutableCompositionTrack音频轨道

FileManagerTool.h 中提供的方法与混拼一样. 在 FileManagerTool.m 中提供实现方法:
- (void)pieceMP3File:(NSArray *)mp3Paths
savePath:(NSString *)filePath
finished:(void(^)(NSString *reslutPath, BOOL isPieceSuccess))finished {
NSMutableArray<AVURLAsset *> *URLAssetMarr = [NSMutableArray array];
// 0.一个合成的工程文件
AVMutableComposition *compositiop = [AVMutableComposition composition];
// 1.利用合成对象创建拼接完成后的音频轨道
AVMutableCompositionTrack *finishedTrack = [compositiop addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:0];
for (NSInteger i = 0; i<mp3Paths.count; i++) {
// 根据音频地址创建相应的音频urlAsset
AVURLAsset *URLAsset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:[mp3Paths objectAtIndex:i]]];
[URLAssetMarr addObject:URLAsset];

           // 根据urlAsset获取相应音频的采集音轨
           AVAssetTrack *assetTrack = [[URLAsset tracksWithMediaType:AVMediaTypeAudio] firstObject];
           // 合成
           CMTime startTime = kCMTimeZero;
           if (i == 0) {
               startTime = kCMTimeZero;
           } else if (i == 1) {
               startTime = [URLAssetMarr objectAtIndex:i-1].duration;
           } else {
               startTime = kCMTimeZero;
               for (NSInteger j = 0; j<i; j++) {
                    CMTime beforeTime = [URLAssetMarr objectAtIndex:j].duration;
                    startTime = CMTimeAdd(startTime, beforeTime);
               }
        }
    
        NSError *error = nil;
        [finishedTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, URLAsset.duration) ofTrack:assetTrack atTime:startTime error:&error];
        if (error) {
            NSLog(@"%@", error);
            self.isPieceSuccess = NO;
        } else {
            self.isPieceSuccess = YES;
        }
    }
        // 合并后的文件导出; 音频文件目前只找到合成m4a类型的
        AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:compositiop presetName:AVAssetExportPresetAppleM4A];
        NSString *outPutFilePath = [filePath stringByAppendingPathComponent:@"pieceResult.m4a"];

        NSLog(@"AVAssetExportSession支持的文件类型: %@", session.supportedFileTypes);

        session.outputURL = [NSURL fileURLWithPath:outPutFilePath];
        session.outputFileType = AVFileTypeAppleM4A;
        [session exportAsynchronouslyWithCompletionHandler:^{
                 NSLog(@"拼接结果: %@", outPutFilePath);
                 if (finished) {
                     finished(outPutFilePath, self.isPieceSuccess);
                 }
        }];
};

结束:以上就是多个音频的混拼与拼接, 其实项目需求还不是单纯的拼接, 要要每一张故事图要对应其录制的音频(一张故事图片,一个音频文件)......自动播放完一张故事图后,自动翻到下一章故事图,然后自动播刚才录制的对应这章故事图的音频文件.....好复杂的描述.......@_@

相关文章

网友评论

    本文标题:多个音频的拼接(不是音频的混拼)

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