iOS 音頻拼接
基本知識
AVAsset
正如官網(wǎng)文檔所說——"AVAsset is an abstract class to represent timed audiovisual media such as videos and sounds. Each asset contains a collection of tracks that are intended to be presented or processed together, each of a uniform media type, including but not limited to audio, video, text, closed captions, and subtitles".
大致意思就是說AVAsset是AVFoundation中的一個抽象類,用來代表多媒體資源,比如,音頻,視頻等。
AVURLAsset
AVURLAsset是AVAsset的子類,是一個具體類,用URL來進行初始化。
AVMutableComposition
AVMutableComposition結(jié)合了媒體數(shù)據(jù),可以看成是track(音頻軌道)的集合,用來合成音視頻。
AVMutableCompositionTrack
AVMutableCompositionTrack用來表示一個track,包含了媒體類型、音軌標識符等信息,可以插入、刪除、縮放track片段。
AVAssetTrack
AVAssetTrack表示素材軌道。
AVAssetExportSession
AVAssetExportSession用來對一個AVAsset源對象進行轉(zhuǎn)碼,并導出為事先設置好的格式。
簡單使用
//MARK:音頻憑借
- (void)audioMergeClick{
//1.獲取本地音頻素材
NSString *audioPath1 = [[NSBundle mainBundle]pathForResource:@"一" ofType:@"mp3"];
NSString *audioPath2 = [[NSBundle mainBundle]pathForResource:@"元" ofType:@"mp3"];
AVURLAsset *audioAsset1 = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:audioPath1]];
AVURLAsset *audioAsset2 = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:audioPath2]];
//2.創(chuàng)建兩個音頻軌道,并獲取兩個音頻素材的軌道
AVMutableComposition *composition = [AVMutableComposition composition];
//音頻軌道
AVMutableCompositionTrack *audioTrack1 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:0];
AVMutableCompositionTrack *audioTrack2 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:0];
//獲取音頻素材軌道
AVAssetTrack *audioAssetTrack1 = [[audioAsset1 tracksWithMediaType:AVMediaTypeAudio] firstObject];
AVAssetTrack *audioAssetTrack2 = [[audioAsset2 tracksWithMediaType:AVMediaTypeAudio]firstObject];
//3.將兩段音頻插入音軌文件,進行合并
//音頻合并- 插入音軌文件
// `startTime`參數(shù)要設置為第一段音頻的時長,即`audioAsset1.duration`, 表示將第二段音頻插入到第一段音頻的尾部。
[audioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset1.duration) ofTrack:audioAssetTrack1 atTime:kCMTimeZero error:nil];
[audioTrack2 insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset2.duration) ofTrack:audioAssetTrack2 atTime:audioAsset1.duration error:nil];
//4. 導出合并后的音頻文件
//`presetName`要和之后的`session.outputFileType`相對應
//音頻文件目前只找到支持m4a 類型的
AVAssetExportSession *session = [[AVAssetExportSession alloc]initWithAsset:composition presetName:AVAssetExportPresetAppleM4A];
NSString *outPutFilePath = [[self.filePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"xindong.m4a"];
if ([[NSFileManager defaultManager] fileExistsAtPath:outPutFilePath]) {
[[NSFileManager defaultManager] removeItemAtPath:outPutFilePath error:nil];
}
// 查看當前session支持的fileType類型
NSLog(@"---%@",[session supportedFileTypes]);
session.outputURL = [NSURL fileURLWithPath:self.filePath];
session.outputFileType = AVFileTypeAppleM4A; //與上述的`present`相對應
session.shouldOptimizeForNetworkUse = YES; //優(yōu)化網(wǎng)絡
[session exportAsynchronouslyWithCompletionHandler:^{
if (session.status == AVAssetExportSessionStatusCompleted) {
NSLog(@"合并成功----%@", outPutFilePath);
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:outPutFilePath] error:nil];
[_audioPlayer play];
} else {
// 其他情況, 具體請看這里`AVAssetExportSessionStatus`.
}
}];
}
- (NSString *)filePath {
if (!_filePath) {
_filePath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
NSString *folderName = [_filePath stringByAppendingPathComponent:@"MergeAudio"];
BOOL isCreateSuccess = [kFileManager createDirectoryAtPath:folderName withIntermediateDirectories:YES attributes:nil error:nil];
if (isCreateSuccess) _filePath = [folderName stringByAppendingPathComponent:@"xindong.m4a"];
}
return _filePath;
}
簡單音頻拼接介紹:
http://www.cocoachina.com/ios/20160922/17624.html