日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

iOS 音频拼接

發布時間:2023/12/10 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS 音频拼接 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

工作中或許會遇到這樣的需求,將兩段不同的音頻合成一個音頻(暫且稱之為音頻拼接),實現起來相對來說不是很難,再介紹如何拼接之前,先了解下AVFoundation下的幾個基本知識點。

基本知識
  • 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源對象進行轉碼,并導出為事先設置好的格式。

    音頻拼接

    介紹完基本知識,接下來就介紹下如何實現音頻拼接。
    1、首先,獲取本地的兩個音頻素材(一首五環之歌和iPhone的默認通知提示音):

    ????NSString?*auidoPath1?=?[[NSBundle?mainBundle]?pathForResource:@"三全音"?ofType:@"mp3"];????NSString?*audioPath2?=?[[NSBundle?mainBundle]?pathForResource:@"五環之歌"?ofType:@"mp3"];????AVURLAsset?*audioAsset1?=?[AVURLAsset?assetWithURL:[NSURL?fileURLWithPath:auidoPath1]];????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、將兩段音頻插入音軌文件進行合并:

    ????//?音頻合并?-?插入音軌文件[audioTrack1?insertTimeRange:CMTimeRangeMake(kCMTimeZero,?audioAsset1.duration)?ofTrack:audioAssetTrack1?atTime:kCMTimeZero?error:nil];[audioTrack2?insertTimeRange:CMTimeRangeMake(kCMTimeZero,?audioAsset2.duration)?ofTrack:audioAssetTrack2?atTime:kCMTimeZero?error:nil];

    4、最后就是用AVAssetExportSession導出合并后音頻文件:

    ????//?合并后的文件導出?-?音頻文件目前只找到合成m4a類型的AVAssetExportSession?*session?=?[[AVAssetExportSession?alloc]?initWithAsset:composition?presetName:AVAssetExportPresetAppleM4A];????NSString?*outPutFilePath?=?[[self.filePath?stringByDeletingLastPathComponent]?stringByAppendingPathComponent:@"xindong.m4a"];session.outputURL?=?[NSURL?fileURLWithPath:outPutFilePath];session.outputFileType?=?AVFileTypeAppleM4A;[session?exportAsynchronouslyWithCompletionHandler:^{????????NSLog(@"合并完成----%@",?outPutFilePath);_player?=?[[AVAudioPlayer?alloc]?initWithContentsOfURL:[NSURL?fileURLWithPath:outPutFilePath]?error:nil];[_player?play];}];

    對于音頻文件的合成導出類型,目前只找到支持.m4a格式的(不知道是否支持其他類型)。對于導出文件所支持的類型可以通過[session supportedFileTypes]進行查看。demo鏈接...

總結

以上是生活随笔為你收集整理的iOS 音频拼接的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。