Sound effect that quiets other audio iOS AVAudio - ios

I want to have a sound effect in my iOS that, if other audio is playing (such as iTunes) quiets any other audio is playing and plays over top of it. I know
AVAudioSessionCategoryOptionMixWithOthers
Will mix the sounds together, but I want mine to be able to be heard clearly. I've seen this done in the RunKeeper app (when the lady describes your run to you it quiets your music) but I can not find it in Apple's Documentation.
Anyone have experience with this?

Apple's documentation says this only works with PlayAndRecord, but it works.
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDuckOthers 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!

iOS: Keep playing music in background with Spotify SDK

I'm using the Spotify iOS SDK and have a question about the background music.
When a user locks the phone or presses the Home button I want my app to continue to play the music. How is this possible?
I've tested
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
Together with adding "Required background modes" to the apps plist.
Does this require that the AVAudioPlayer is used? Since I'm using the Spotify streamingPlayer its not possible.
Background playback should work just fine. You do need the audio item in your UIBackgroundModes Info.plist entry, though. Also, make sure you test on a device - I know remote control events don't work in the iOS Simulator, and that might be the case for background audio as well.
The iOS SDK will automatically set up the AVAudioSession for you, so you don't need to do that.
You might find this answer helpful, which is more in-depth: Background Audio with cocoalibspotify.

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];

How do I stop AirPlay audio with AVPlayer?

I'm working on an iOS app that, for reasons we won't get into here, wants to sometimes disable AirPlay playback on a remote device, and only play video locally on the iOS device, regardless of the user's settings.
I'm using AVPlayer to play the video, and that's currently non-negotiable.
I'm setting the iOS 5.0+ AVPlayer property allowsAirPlayVideo to NO, which prevents the video from playing remotely. But if the user has turned on AirPlay on the iOS device, the audio is still streamed through the remote device.
I don't see a comparable allowsAirPlayAudio flag in Apple's documentation, nor have I found mention of this issue anywhere else.
For example, this Stack Overflow question:
Audio Output Routes for AirPlay
talks about "audio output destinations in a USB audio accessory", which doesn't sound like what I need.
I don't want to just turn off audio, I want the audio to keep being played through my iOS device.
Am I missing something? Thanks!
Possible a duplicate of How to detect if a bluetooth headset plugged or not IOS 8?
I know it's an old question, but hey, it may help some one
This is what I do to disconnect the audio, mConnectDisconnect is boolean parameter.
allowsExternalPlayback will remove the image from airplay and play it on your device screen.
AVAudioSessionCategoryOptionDefaultToSpeaker this option will let you change audio to the device
AVAudioSession* session = [AVAudioSession sharedInstance];
self.player.allowsExternalPlayback = mConnectDisconnect;
if(allowsExternalPlayback){
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowAirPlay error:nil];
}else{
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
}
[session setActive:true error:nil];
}

Playing sounds while ipod music is on (iPad)

What is the best pattern for an application to play sounds while enabling user to use his iPad app to play music as well? Right now if the music is playing any sound played by my app will stop the music. Is there a way to disable sounds while ipod is playing?
By default, sounds played interrupt iPod, as you have seen. In order to tell the system that you want the sounds you're playing to be mixed in with other sounds on the system, such as iPod, you need to set the category of your AVAudioSession to AVAudioSessionCategoryAmbient, like this:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
Do this before you begin playing sounds, and you should get the desired effect. Here's the documentation:
http://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html#//apple_ref/doc/uid/TP40008240-CH1-SW1

Resources