iOS音頻合成(音頻拼接)

iOS 音頻拼接

基本知識
AVAsset
正如官網文檔所說——"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結合了媒體數據,可以看成是track(音頻軌道)的集合,用來合成音視頻。
AVMutableCompositionTrack
AVMutableCompositionTrack用來表示一個track,包含了媒體類型、音軌標識符等信息,可以插入、刪除、縮放track片段。
AVAssetTrack
AVAssetTrack表示素材軌道。
AVAssetExportSession
AVAssetExportSession用來對一個AVAsset源對象進行轉碼,并導出為事先設置好的格式。

簡單使用

//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.創建兩個音頻軌道,并獲取兩個音頻素材的軌道
    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`參數要設置為第一段音頻的時長,即`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;   //優化網絡
    [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

音頻與音頻, 音頻與視頻

http://www.lxweimin.com/p/76e0c4e684db

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 在此特此聲明:一下所有鏈接均來自互聯網,在此記錄下我的查閱學習歷程,感謝各位原創作者的無私奉獻 ! 技術一點一點積...
    遠航的移動開發歷程閱讀 11,230評論 12 197
  • 不知不覺2017年的余額已經所剩無幾了 下面是我這一年來收藏的關于IOS開發的一些知識點 . iOS功能 iOS ...
    臨淵還在閱讀 699評論 0 0
  • iOS功能 iOS 如何跳轉到系統設置里的指定子功能界面 http://blog.csdn.net/jingfa1...
    EmmaLyx閱讀 698評論 0 4
  • 有一段時間沒有畫《超級個體》的筆記了,今天下午畫了一張圖: 說到優勢,通過畫圖和接觸工作上的一些事情,發生自己比較...
    smallfen閱讀 322評論 5 3
  • 雖說,現在是新時代,但是很多農村里的老一輩還是舊思想的執行者。你可以想象,在日落西山,閑暇之時,圍坐一團,家...
    拾起獨處時光閱讀 474評論 2 5