Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to record video and maintain music playing in the background?

Sorry if this is a duplicate, but I couldn't find a question similar to this. I have a custom camera/recorder that I made with AVFoundation and I was wondering how to keep the audio running from other apps while recording a video because right now it stops the audio (doesn't even pause it) and then records the video

If I am thinking correctly, could this be solved by adding something similar to this:

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
like image 543
Lehman2020 Avatar asked Aug 06 '15 16:08

Lehman2020


People also ask

Can I play music in the background while recording a video?

Visit Play Store and install Play Music While Recording app. Start playing music you want to hear while recording. Open the Installed app and stay in Video mode. Start recording your video by tap on camera icon.

How do I keep music playing in the background?

From the Home screen, tap Apps > Music Player . Tap a song in your library to listen to it. Tap the Menu Key > Settings and checkmark the Show notification option so that the music controller is displayed on the Notifications panel.


2 Answers

Sample Code:

AVAudioSession *session = [AVAudioSession sharedInstance];
session.delegate = self;

NSError *error = nil;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];

OSStatus propertySetError = 0;

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
propertySetError = AudioSessionSetProperty ( 
    kAudioSessionProperty_OverrideAudioRoute,                         
    sizeof (audioRouteOverride),                                      
    &audioRouteOverride                                               
); 
[session setActive:YES error:&error];
like image 136
Bhavin Avatar answered Oct 17 '22 01:10

Bhavin


This fixed it.


[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
                                 withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionMixWithOthers
                                       error:nil];
like image 40
Lehman2020 Avatar answered Oct 16 '22 23:10

Lehman2020