Record sound on iOS without stopping music playback

When recording audio with audio playback, AVAudioSession from a music application or other files stops. This is described in the audio documentation:

AVAudioSessionCategoryRecord

To record sound; this category disables audio playback.

Is there a way to change this behavior and do one of the following?

1) Continue playing the sound of other applications and let the phone record the sound being played.

2) Resume the sound of other applications after completing a short recording. Notification applications can respond when sound interruptions begin or end. Is there anything specific that needs to be done for other applications to receive these notifications?

+6
source share
3 answers

1) AVAudioSession possible for AVAudioSession . All you can do is check [[AVAudioSession sharedInstance] isOtherAudioPlaying] and ask the user if he wants to stop streaming music or not.

2) The application cannot receive any notifications when the status of the iPod changes, we cannot even observe the isOtherAudioPlaying property ... The solution I use in my projects: add NSTimer and check the [[AVAudioSession sharedInstance] isOtherAudioPlaying] BOOL value once a second.

0
source

you can use

Obj-c

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; 

or

Swift

 let audioSession = AVAudioSession.sharedInstance() do { try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.mixWithOthers) } catch let error as NSError { print("audioSession error: \(error.localizedDescription)") } 
0
source

with 24 hours of diligent search and fragmentation of things, it is surprisingly simple:

 let session = AVAudioSession.sharedInstance() session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.mixWithOthers, .allowBluetoothA2DP]) 

And this is for anyone who is still looking for how to do it.

0
source

All Articles