[英]How to Export an audio file with effect in iOS
我使用AVAudioEngine播放具有效果的多音頻文件。 我想導出此聲音(包括效果和正在播放的多個文件)而不進行記錄。
我該怎么做?
這是我使用AVAudioEngine的代碼
//1. Create engine
_audioEngine = [[AVAudioEngine alloc] init];
//2. Create player nodes , unit
_mainPlayer = [[AVAudioPlayerNode alloc] init];
_aPlayer = [[AVAudioPlayerNode alloc] init];
_bPlayer = [[AVAudioPlayerNode alloc] init];
_effectTimePitch = [[AVAudioUnitTimePitch alloc] init];
AVAudioMixerNode *mixerNode = [[AVAudioMixerNode alloc] init];
//3. Attach node to engine
[_audioEngine attachNode:_mainPlayer];
[_audioEngine attachNode:_aPlayer];
[_audioEngine attachNode:_bPlayer];
[_audioEngine attachNode:mixerNode];
[_audioEngine attachNode:_effectTimePitch];
//4. Connect player node to engine's main mixer
NSDictionary *setting = @{
AVFormatIDKey: @(AVAudioPCMFormatFloat32),
AVSampleRateKey: @(1),
AVNumberOfChannelsKey: @(1)
};
_audioFormat = [[AVAudioFormat alloc] initWithSettings:setting];
[_audioEngine connect:_mainPlayer to:mixerNode format:_audioFormat];
[_audioEngine connect:_aPlayer to:mixerNode format:_audioFormat];
[_audioEngine connect:_bPlayer to:mixerNode format:_audioFormat];
[_audioEngine connect:mixerNode to:_effectTimePitch format:_audioFormat];
[_audioEngine connect:_effectTimePitch to:_audioEngine.mainMixerNode format:_audioFormat];
//5. Start engine
NSError *error;
NSAssert([_audioEngine startAndReturnError:&error], @"couldn't start engine, %@", [error localizedDescription]);
//6 Play Audio
AVAudioFile *audioFile = [[AVAudioFile alloc] initForReading: _mainURL error: nil];
[_mainPlayer scheduleFile:audioFile atTime:nil completionHandler:nil];
AVAudioFile *aFile = [[AVAudioFile alloc] initForReading: _mainURL error: nil];
[_aPlayer scheduleFile:aFile atTime:nil completionHandler:nil];
AVAudioFile *bFile = [[AVAudioFile alloc] initForReading: _mainURL error: nil];
[_bPlayer scheduleFile:bFile atTime:nil completionHandler:nil];
[_mainPlayer play];
[_aPlayer play];
[_bPlayer play];
您必須設置一個AVAudioFile進行寫入。 然后,在鏈中某個較晚的點(例如最后一個效果節點)上安裝一個水龍頭,然后從該緩沖區寫入輸出文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.