在音频文件中记录我的应用程序生成的所有声音(而不是从麦克风)

Vai*_*arg 9 audio xcode avaudiorecorder ios

我有一个像仪器一样的屏幕.有按钮播放声音文件.

我想记录当用户按下单个音频文件中的按钮时播放的声音,以便我可以将该文件保存为mp4或其他音频格式.

能否指导我如何以简单的方式实现这一目标?

我能用麦克风录音 AVAudioRecorder

我认为,录音方法使用麦克风作为源,但我希望它使用我的应用程序的"音频输出"等效作为源.

liu*_*ut0 6

您可以尝试使用The Amazing Audio Engine.您可以通过Cocoapods安装它

pod 'TheAmazingAudioEngine'
Run Code Online (Sandbox Code Playgroud)

或通过git克隆

git clone --depth=1 https://github.com/TheAmazingAudioEngine/TheAmazingAudioEngine.git
Run Code Online (Sandbox Code Playgroud)

借助于声音,声音可以记录到文件中.

因此,如果您想记录应用输出,只需使用Outputreceiver:

@property (nonatomic, strong) AEAudioController *audioController;
@property (nonatomic, strong) AERecorder *recorder;

...

self.audioController = [[AEAudioController alloc] initWithAudioDescription:[AEAudioController nonInterleaved16BitStereoAudioDescription] inputEnabled:YES];

...

//start the recording
- (void) beginRecording{
   self.recorder = [[AERecorder alloc] initWithAudioController:self.audioController];
   NSString *documentsFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

   //the path to record to
   NSString *filePath = [documentsFolder stringByAppendingPathComponent:@"AppOutput.aiff"];

  //start recording
  NSError *error = NULL;
  if ( ![_recorder beginRecordingToFileAtPath:filePath fileType:kAudioFileAIFFType error:&error] ) {
         //an error occured
         return;
  }

   [self.audioController addOutputReceiver:self.recorder];
}

...

//end the recording
- (void)endRecording {
     [self.audioController removeOutputReceiver:self.recorder];
     [self.recorder finishRecording];
}
Run Code Online (Sandbox Code Playgroud)