2018-06-17

音樂合并,混音

https://stackoverflow.com/questions/7656512/combining-two-caf-files-on-iphone



- (void) setUpAndAddAudioAtPath:(NSURL*)assetURL toComposition:(AVMutableComposition *)composition {

? ? 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 = CMTimeMakeWithSeconds(0, 1);

? ? CMTime trackDuration = songAsset.duration;

? ? CMTime longestTime = CMTimeMake(848896, 44100); //(19.24 seconds)

? ? 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

? ? ok = [track insertTimeRange:tRange ofTrack:sourceAudioTrack atTime:CMTimeMake(0, 44100) error:&error];

}?




- (void) exportAudio {

? ? AVMutableComposition *composition = [AVMutableComposition composition];

? ? audioMixParams = [[NSMutableArray alloc] initWithObjects:nil];

? ? //Add Audio Tracks to Composition

? ? NSString *URLPath1 = pathToYourAudioFile1;

? ? NSURL *assetURL1 = [NSURL fileURLWithPath:URLPath1];

? ? [self setUpAndAddAudioAtPath:assetURL1 toComposition:composition];

? ? NSString *URLPath2 = pathToYourAudioFile2;

? ? NSURL *assetURL2 = [NSURL fileURLWithPath:URLPath2];

? ? [self setUpAndAddAudioAtPath:assetURL2 toComposition:composition];

? ? 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 = [[util getDocumentsDirectory] stringByAppendingFormat: @"/%@.m4a", fileName];? ?

? ? // set up export

? ? myDeleteFile(exportFile);

? ? 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];

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末霎奢,一起剝皮案震驚了整個濱河市咳榜,隨后出現(xiàn)的幾起案子拄养,更是在濱河造成了極大的恐慌溪王,老刑警劉巖沉迹,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件浪谴,死亡現(xiàn)場離奇詭異开睡,居然都是意外死亡因苹,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進(jìn)店門篇恒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來扶檐,“玉大人,你說我怎么就攤上這事婚度≌好兀” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵蝗茁,是天一觀的道長醋虏。 經(jīng)常有香客問我,道長哮翘,這世上最難降的妖魔是什么颈嚼? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任靡馁,我火速辦了婚禮冯痢,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘旁趟。我一直安慰自己艰匙,他們只是感情好限煞,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著员凝,像睡著了一般署驻。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上健霹,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天旺上,我揣著相機(jī)與錄音,去河邊找鬼糖埋。 笑死宣吱,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的瞳别。 我是一名探鬼主播征候,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼祟敛!你這毒婦竟也來了倍奢?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤垒棋,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后痪宰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體叼架,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡畔裕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了乖订。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片扮饶。...
    茶點(diǎn)故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖乍构,靈堂內(nèi)的尸體忽然破棺而出甜无,到底是詐尸還是另有隱情,我是刑警寧澤哥遮,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布岂丘,位于F島的核電站,受9級特大地震影響眠饮,放射性物質(zhì)發(fā)生泄漏奥帘。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一仪召、第九天 我趴在偏房一處隱蔽的房頂上張望寨蹋。 院中可真熱鬧,春花似錦扔茅、人聲如沸已旧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽运褪。三九已至,卻和暖如春萤晴,著一層夾襖步出監(jiān)牢的瞬間吐句,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工店读, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留嗦枢,地道東北人。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓屯断,卻偏偏與公主長得像文虏,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子殖演,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容