iOS AVAudioRecorder record without stopping background audio - ios

I need to allow my app to record audio without stopping background music player.
I'm using AVAudioRecorder.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
However, anytime [audioplayer record] is called, the background music will stop playing.
What can I do so that background music doesn't stop playing?
Thanks.

You need to add the MixWithOthers Category Option as well:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:nil];

Related

How can you configure AVAudioSession to duck music and pause spoken audio?

I'm working on a navigation app that uses AVSpeechSynthesizer to call out directions. If I'm playing a podcast I would like to be able to pause the audio while a direction is spoken and resume it afterwards. This is successfully achieved using:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers
error:nil];
However, if I play music using the above code the instruction gets mixed with the music without the volume of the music being reduced - making it difficult to hear the instruction. So I tried:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDuckOthers
error:nil];
Now the background music reduces in volume, which is what I want, but when I play spoken audio (i.e. a podcast) it is no longer paused and instead gets mixed with the instruction - albeit at a reduced volume.
What I really want is something like: AVAudioSessionCategoryOptionInterruptSpokenAudioAndDuckWithOthers. However, that doesn't exist. I know that this combination is possible, because other apps (like Waze) have this behaviour.
I know that you can detect when audio is playing using:
BOOL isOtherAudioPlaying = [[AVAudioSession sharedInstance] isOtherAudioPlaying];
But, I couldn't find a way of determining if that Audio was spoken or not. If I could I could set things up according to what is playing at any given time. Can anyone help with this, preferably in Objective C?
This is how it's done:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDuckOthers | AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers
error:nil];
Works a charm!

AVAudioSession setCategory not working

I have a video capturing app and I want to be able to play background music while recording audio+video.
I can accomplish this if I set the AVAudioSession category to PlayAndRecord in didFinishLaunchingWithOptions. However, this causes a glitch in the audio whenever the view with the camera enters or exits the foreground, and its apparently impossible to get rid of: https://forums.developer.apple.com/message/74778#74778
I can live with the glitch if it just happens when I start/stop recording video, but that means I need to change the AVAudioSession category from Ambient to PlayAndRecord when a button is pressed, and this also seems to not be possible.
How do I change the AVAudioSession category?
I tried this:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:nil];
But the background music stops when the recording starts.
Following this post: AVCaptureSession and background audio iOS 7
I tried deactivating the session then setting the category:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
[session setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker
error:nil];
[session setActive:YES error:&error];
I also tried listening for the AVAudioSessionRouteChangeNotification and setting the category in that handler after deactivating the session. But the background music always turns off when the app starts recording.
Why can't I change the AVAudioSession category?
Please try this category
AVAudioSessionCategoryMultiRoute
or check both categories
AVAudioSessionCategoryPlayAndRecord | AVAudioSessionCategoryRecord

How can I play a video without causing music on iTunes/other audio to pause

I'm using AVPlayer to play a video (open to alternative players). My videos don't have sound. When the video starts, if the IOS device is playing music/audio of some sort, the music/audio pauses even though the video has no sound.
Is there any way to continue playing music while playing a silent movie?
Try preparing the audio session before starting playback:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
[[AVAudioSession sharedInstance] setActive:NO error:nil];
//... create & configure AVPlayer and start playback

iOS MPMoviePlayerViewController stop playing when screen locks

I implement MPMoviePlayerViewController to play video and I enable airPlay and works great but the problem is when the screen on the ipad locks. My question is how can make sure the MPMoviePlayerViewController still in airPlay even when the screen locks. Any of you knows how can I make this work?
Where as Apple thinks that When user can't see the video what is the benefit to runs consistently? So there is no need to play Video when you can't see it. Apple pauses it when app goes to background or screen locks. If you still want to play it you can add notification to start and stop when you go to background and come to foreground.
Hope this helps.
In your Info.plist file, add the key "Required background modes" with the value "App plays audio or streams audio/video using AirPlay".
Also, when you receive the notification MPMoviePlayerIsAirPlayVideoActiveDidChangeNotification, add these lines:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
And add this line to your dealloc method:
[[AVAudioSession sharedInstance] setActive:NO error:nil];

Pausing song when a video begins playing (and vice-versa)

I have an app that uses AVPlayer to play songs and a UIWebView to play YouTube videos. When I build it against iOS 5, the audio and video refuse to play together, which I like. Pressing play on one will automatically pause the other, with a nice half-second fade-out.
Now that I'm building against iOS 6, this behavior is gone -- the songs and videos play over each other. How can I get back the iOS 5 behavior?
add the shared categories in your App delegate:
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
this will limit to only one active audio session

Resources