前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS 多条音频拼接为一条音频进行播放

iOS 多条音频拼接为一条音频进行播放

作者头像
freesan44
发布2021-12-06 19:34:53
6730
发布2021-12-06 19:34:53
举报
文章被收录于专栏:freesan44freesan44

场景

把多条mp3音频合并为一条保存并进行播放

解决方案

  1. 首先把全部音频路径生成为一个数组:
代码语言:javascript
复制
NSMutableArray * fileUrlArr = @[].mutableCopy;
    [mp3NameArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *mp3Name = [NSString stringWithFormat:@"%@",obj];
    
        // mp3路径
        NSString *audioFileURL = [[NSBundle mainBundle] pathForResource:mp3Name ofType:@"mp3"];
        [fileUrlArr addObject:audioFileURL];
    }];
  1. 通过以下方法合并音频,保存在一个随机文件中,因为文件如果已存在或者文件目录写入失败,会出现【AVAssetExportSessionStatusFailed】错误码
代码语言:javascript
复制
///合并音频
- (void) mergeAVAssetWithSourceURLs:(NSArray *)sourceURLsArr completed:(void (^)(NSString * outputFileUrlStr)) completed{
    //创建音频轨道,并获取多个音频素材的轨道
    AVMutableComposition *composition = [AVMutableComposition composition];
    //音频插入的开始时间,用于记录每次添加音频文件的开始时间
    __block CMTime beginTime = kCMTimeZero;
    [sourceURLsArr enumerateObjectsUsingBlock:^(id  _Nonnull audioFileURL, NSUInteger idx, BOOL * _Nonnull stop) {
        //获取音频素材
        AVURLAsset *audioAsset1 = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:audioFileURL]];
        //音频轨道
        AVMutableCompositionTrack *audioTrack1 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:0];
        //获取音频素材轨道
        AVAssetTrack *audioAssetTrack1 = [[audioAsset1 tracksWithMediaType:AVMediaTypeAudio] firstObject];
        //音频合并- 插入音轨文件
        [audioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset1.duration) ofTrack:audioAssetTrack1 atTime:beginTime error:nil];
        // 记录尾部时间
        beginTime = CMTimeAdd(beginTime, audioAsset1.duration);
    }];
    //导出合并后的音频文件
    //音频文件目前只找到支持m4a 类型的
    AVAssetExportSession *session = [[AVAssetExportSession alloc]initWithAsset:composition presetName:AVAssetExportPresetAppleM4A];
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss-SSS"];
    NSString * timeFromDateStr = [formater stringFromDate:[NSDate date]];
    NSString *outPutFilePath = [NSHomeDirectory() stringByAppendingFormat:@"/tmp/sound-%@.mp4", timeFromDateStr];
    
    
    // 音频文件输出
    session.outputURL = [NSURL fileURLWithPath:outPutFilePath];
    session.outputFileType = AVFileTypeAppleM4A; //与上述的`present`相对应
    session.shouldOptimizeForNetworkUse = YES;   //优化网络
    [session exportAsynchronouslyWithCompletionHandler:^{
        if (session.status == AVAssetExportSessionStatusCompleted) {
            NSLog(@"合并成功----%@", outPutFilePath);
            if (completed) {
                completed(outPutFilePath);
            }
        } else {
            // 其他情况, 具体请看这里`AVAssetExportSessionStatus`.
            NSLog(@"合并失败----%ld", (long)session.status);
            if (completed) {
                completed(nil);
            }
        }
    }];
}
  1. 输出合并音频
代码语言:javascript
复制
// 合并音频文件生成新的音频
    [self mergeAVAssetWithSourceURLs:musicArr completed:^(NSString *outputFileUrlStr) {
        if (!outputFileUrlStr) {
            NSLog(@"声音生成失败!");
            return;
        }
            self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:outputFileUrlStr] error:nil];
            [self.audioPlayer play];
    }];

参考: https://www.cxyzjd.com/article/ismilesky/52780349 https://www.jianshu.com/p/3e357e3129b8 http://www.cocoachina.com/articles/17624

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021/11/12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 场景
  • 解决方案
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档