ios - 如何在 iOS 中导出具有效果的音频文件

标签 ios audio avaudioengine

我使用 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。然后在链中的某个后期点(例如最后一个效果节点)安装一个 tap,然后从该缓冲区写入输出文件。

关于ios - 如何在 iOS 中导出具有效果的音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30746915/

相关文章:

ios - AVAudioEngine AudioFile 时间管理

ios - Swift 4 - 对齐两列 UILabel View

iphone - 没有AfterDelay的时间后,我该如何执行selector? (里面的代码)

ios - 如何调用drawRect : when layoutSubviews invokes in ios

ios - 如何从同一个 AVAudioEngine 播放多个不同的音频文件? (错误: !nodeimpl->HasEngineImpl())

swift - 插入/拔出耳机时 SKAudioNode() 崩溃

ios - Xcode : Expected method to write array element not found on object of type 'NSArray *' 错误

ios - ios:如何在图库的应用程序背景中播放歌曲

ios - xamarin iOS : How to show the audio amplitude of the voice when recording

node.js - 使用nodejs REST API播放音频?