在目标 c 中将两个音频文件合二为一 [英] Combine two audio files into one in objective c

查看:30
本文介绍了在目标 c 中将两个音频文件合二为一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
在 iPhone 上合并音频文件

我正在尝试将 2 个音频文件合并为一个文件.例如:file1.mp3 - 它说我是".file2.mp3 - 它说乔治".我想得到一个组合文件 file3.mp3,它会说我是乔治".

我如何在 Objective-C 中做到这一点?

解决方案

看看 AVFoundation 框架 ... 有很多方法,但对你来说最简单的方法是 ...

  • 为两个文件创建AVAsset(使用AVURLAsset 子类),
  • alloc AVMutableComposition (composition),
  • AVMutableCompositionTrack 类型为 AVMediaTypeAudio 添加到 composition

[composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

  • 从第一个 AVAsset 中获取音轨并添加AVMutableCompositionTrack
  • 从第二个AVAsset中获取音轨并将其附加到AVMutableCompositionTrack
  • 然后使用您的作品创建 AVAssetExportSession 并将其导出.

简化的描述,但你会得到一个线索.取决于你有多少曲目,你想使用什么样的效果等.

如果您确实想查看一些真实的代码,请打开 AVMovieExporter 示例,复制此代码并删除视频内容并仅保留音频.

Possible Duplicate:
merge Audio files on iPhone

I'm trying to combine 2 audio files into one file. For example: file1.mp3 - it says "I am". file2.mp3 - it says "George". I want to get a combined file file3.mp3 which will say "I am George".

How can I do this in objective-C?

解决方案

Look at AVFoundation framework ... There're many ways, but the simplest one for you can be ...

  • create AVAsset for both files (use AVURLAsset subclass),
  • alloc AVMutableComposition (composition),
  • add AVMutableCompositionTrack with type AVMediaTypeAudio to composition

[composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

  • get track(s) from the first AVAsset and add it AVMutableCompositionTrack,
  • get track(s) from the second AVAsset and append it to AVMutableCompositionTrack,
  • then create AVAssetExportSession with your composition and export it.

Simplified description, but you get a clue. Depends on how many tracks do you have, what kind of effects do you want to use, etc.

If you do want to see some real code, open AVMovieExporter example, copy this code and remove video stuff and leave audio there only.

这篇关于在目标 c 中将两个音频文件合二为一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆