- (void) setUpAndAddAudioAtPath:(NSURL*)assetURL toComposition:(AVMutableComposition *)composition start:(CMTime)start dura:(CMTime)dura offset:(CMTime)offset{
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *sourceAudioTrack = [[songAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
NSError *error = nil;
BOOL ok = NO;
CMTime startTime = start;
CMTime trackDuration = dura;
CMTimeRange tRange = CMTimeRangeMake(startTime, trackDuration);
//Set Volume
AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
[trackMix setVolume:0.8f atTime:startTime];
[audioMixParams addObject:trackMix];
//Insert audio into track //offset CMTimeMake(0, 44100)
ok = [track insertTimeRange:tRange ofTrack:sourceAudioTrack atTime:offset error:&error];
}
- (void) exportAudio {
AVMutableComposition *composition = [AVMutableComposition composition];
audioMixParams = [[NSMutableArray alloc] initWithObjects:nil];
//Add Audio Tracks to Composition
NSString *path = [[NSBundle mainBundle] pathForResource:@"recordedFile" ofType:@"caf"];
NSURL *assetURL1 = [NSURL fileURLWithPath:path];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL1 options:nil];
CMTime startTime = CMTimeMakeWithSeconds(0, 1);
CMTime trackDuration = songAsset.duration;
[self setUpAndAddAudioAtPath:assetURL1 toComposition:composition start:startTime dura:trackDuration offset:CMTimeMake(14*44100, 44100)];
path = [[NSBundle mainBundle] pathForResource:@"1286" ofType:@"mp3"];
NSURL *assetURL2 = [NSURL fileURLWithPath:path];
[self setUpAndAddAudioAtPath:assetURL2 toComposition:composition start:startTime dura:trackDuration offset:CMTimeMake(0, 44100)];
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
audioMix.inputParameters = [NSArray arrayWithArray:audioMixParams];
//If you need to query what formats you can export to, here's a way to find out
NSLog (@"compatible presets for songAsset: %@",
[AVAssetExportSession exportPresetsCompatibleWithAsset:composition]);
AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
initWithAsset: composition
presetName: AVAssetExportPresetAppleM4A];
exporter.audioMix = audioMix;
exporter.outputFileType = @"com.apple.m4a-audio";
NSString *fileName = @"someFilename";
NSString *exportFile = [NSHomeDirectory() stringByAppendingFormat: @"/%@.m4a", fileName];
// set up export
if ([[NSFileManager defaultManager] fileExistsAtPath:exportFile]) {
[[NSFileManager defaultManager] removeItemAtPath:exportFile error:nil];
}
NSURL *exportURL = [NSURL fileURLWithPath:exportFile];
exporter.outputURL = exportURL;
// do the export
[exporter exportAsynchronouslyWithCompletionHandler:^{
int exportStatus = exporter.status;
switch (exportStatus) {
case AVAssetExportSessionStatusFailed:{
NSError *exportError = exporter.error;
NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
break;
}
case AVAssetExportSessionStatusCompleted: NSLog (@"AVAssetExportSessionStatusCompleted"); break;
case AVAssetExportSessionStatusUnknown: NSLog (@"AVAssetExportSessionStatusUnknown"); break;
case AVAssetExportSessionStatusExporting: NSLog (@"AVAssetExportSessionStatusExporting"); break;
case AVAssetExportSessionStatusCancelled: NSLog (@"AVAssetExportSessionStatusCancelled"); break;
case AVAssetExportSessionStatusWaiting: NSLog (@"AVAssetExportSessionStatusWaiting"); break;
default: NSLog (@"didn't get export status"); break;
}
}];
// // start up the export progress bar
// progressView.hidden = NO;
// progressView.progress = 0.0;
// [NSTimer scheduledTimerWithTimeInterval:0.1
// target:self
// selector:@selector (updateExportProgress:)
// userInfo:exporter
// repeats:YES];
}
IOS 音頻合成
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門(mén)霹期,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)叶组,“玉大人,你說(shuō)我怎么就攤上這事历造∷κ” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵吭产,是天一觀的道長(zhǎng)侣监。 經(jīng)常有香客問(wèn)我,道長(zhǎng)臣淤,這世上最難降的妖魔是什么橄霉? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮邑蒋,結(jié)果婚禮上姓蜂,老公的妹妹穿的比我還像新娘按厘。我一直安慰自己,他們只是感情好覆糟,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布刻剥。 她就那樣靜靜地躺著遮咖,像睡著了一般滩字。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上御吞,一...
- 那天麦箍,我揣著相機(jī)與錄音,去河邊找鬼陶珠。 笑死挟裂,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的揍诽。 我是一名探鬼主播诀蓉,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼暑脆!你這毒婦竟也來(lái)了渠啤?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤添吗,失蹤者是張志新(化名)和其女友劉穎沥曹,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體碟联,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡妓美,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了鲤孵。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片壶栋。...
- 正文 年R本政府宣布锡移,位于F島的核電站,受9級(jí)特大地震影響漆际,放射性物質(zhì)發(fā)生泄漏淆珊。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一奸汇、第九天 我趴在偏房一處隱蔽的房頂上張望施符。 院中可真熱鬧往声,春花似錦、人聲如沸戳吝。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)听哭。三九已至慢洋,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間陆盘,已是汗流浹背普筹。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像酸员,于是被迫代替她去往敵國(guó)和親蜒车。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- iOS 音頻拼接 簡(jiǎn)單使用 簡(jiǎn)單音頻拼接介紹: http://www.cocoachina.com/ios/201...
- 這個(gè) 給我的感覺(jué)就像是 PPT 播放一樣。這里找了一些資料崭添,學(xué)習(xí)視頻合成方面的知識(shí)寓娩。 一:圖片和視頻的合成: @i...
- 今天和大家一起分享關(guān)于音軌合成的坑(套路!全TM是套路!走一步套一路) 理論部分: 我們需要用到AVURLAsse...