Oren
Oren

Reputation: 5103

Record Audio from another app playing in background

I'm looking for a way to record audio that's playing in the background from another app. For example, being able to record what Pandora/Spotify is playing.

From what I understand this isn't possible yet Snapchat's latest update supports background audio when recording a video.

This post shows how to allow audio to continue in the background however as soon as I set [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil]; the background audio stops immediately.

How does Snapchat allow the background audio to continue while recording?

Upvotes: 4

Views: 1512

Answers (1)

AndroC
AndroC

Reputation: 4884

When initializing AVCaptureSession set its property:

    AVCaptureSession *session = [[AVCaptureSession alloc] init];
       ...
    session.automaticallyConfiguresApplicationAudioSession = NO;

Before starting your video recording, set your AVAudioSession category:

if ([AVAudioSession sharedInstance].category != AVAudioSessionCategoryPlayAndRecord) {
   [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
   [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:(AVAudioSessionCategoryOptionDefaultToSpeaker |  AVAudioSessionCategoryOptionDuckOthers) error:nil];
   [[AVAudioSession sharedInstance] setActive:YES error:nil];
}

Upvotes: 2

Related Questions