AVAudioPlayer turns off iPod - how to work around? - iphone

I use AVAudioPlayer to play sounds in my app, but it turns off the iPod when a sound plays.
Is there a way to prevent this? I don't want to use System Sounds because I can't control their volume.
Thanks for your help.

This should work for you (taken from my Ambiance app)
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
From the docs:
kAudioSessionProperty_OverrideCategoryMixWithOthers
Changes the mixing behavior of the
kAudioSessionCategory_MediaPlayback
and
kAudioSessionCategory_PlayAndRecord
audio session categories. Setting this
property to TRUE (any nonzero value)
allows mixing of iPod audio with
application audio. Other aspects of
these categories, such as their
Ring/Silent switch behavior, are not
affected.
This property has value of FALSE (0)
by default. When the audio session
category changes, such as during an
interruption, the value of this
property reverts to FALSE. To regain
mixing behavior you must then re-set
this property.
Always check to see if setting this
property succeeds or fails, and react
appropriately; behavior may change in
future releases of iPhone OS.
Available in iPhone OS 3.0 and later.
Declared in AudioServices.h.

Related

unable to get the Audio output in iOS

I have a codebase which plays audio and video perfectly. I have converted that into a small framework and started using it as a part of another code. When i do this, there is no audio output.... I am unable to hear any audio output while it is being played. All the Audio player delegate methods are being called but there is no sound. The same is happening with Videos as well. All the videos are being played with out sound. Its like watching a silent movie.... Any ideas? I am using AVAudioPlayer for playing audio files and MPMoviePlayerController for playing videos.
edit : This happens only on device. It works all fine in simulator
Just came across this post, I was doing the same thing, and actually AVAduioSession is needed to play audio thru speaker/headset. So having AVAudioSession is a must.
//REALLY NEED THIS LINE FOR MUSIC TO PLAY TO SPEAKER
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
and for background playing, add these 2 lines and change setting in plist to allow background mode
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
So stupid of me.... The problem was with the volume button on iPad. The volume button in iPad with iOS 5 has some weird bug. It does not update itself when we toggle the button between screen lock and audio output. I guess this is bug from apple and filed a bug. Hope they solve this in their next update.

AudioSession - want to stop everything being played in background?

I got a need to stop (or mute at least) music/sound that is played in iPhone.
Important: I want my app will do that it even if is in background-state!
I'm using:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategorySoloAmbient error:nil]];
[[AVAudioSession sharedInstance] setActive: YES];
The issue is that everything is being stopped, even streaming music from some other app, but only if app is in FOREGROUND. As wrote before, I want it to be working also in BACKGROUND.
I did simple research and realized it's somehow possible, these apps prove:
App Store - Streaming Music Timer or
App Store - Music Sleep Timer
I guess my solution with SoloAmbient can be not so perfect and it may be a wrong way.
Does anybody know how could I stop/sleep/pause/mute global music even if app will be in background state?
These apps I pointed out are doing basically this thing...
You need to enable the audio background mode.
Add the Required Background Modes key to your app's Info.plist and add the App Plays Audio key to it.
See this tutorial for more.
EDIT Also, you probably want the AVAudioSessionCategoryPlayback category, not AVAudioSessionCategorySoloAmbient

System Sound Lock

I am developing an iPhone app that translates voice to text.
The component for the Transcribe is third-party.
Until now everything worked just fine.
Today I tried playing a sound (.wav file) after making the Transcription and noticed that I cannot play anything (by AVAudio or system sound). After debugging I found out that this is happening after initing the third-party component; so I think that the component is not releasing something that belongs to the system audio.
My question is : Is there a way to force the iPhone play the sound that I want?
Have you tried setting AVAudioSession category?
NSError *err = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&err];
Maybe other category will work, for example AVAudioSessionCategoryPlayAndRecord
The best way IMO is to save AVAudioSession state (its category, for example) before using your third-party component and restore state afterwards.

Checking Microphone Volume in iPhone

How to check whether microphone is muted before recording audio in iPhone?
Actually, you don't have to. You have to "warn" AVAudioSession that you are going to record a bit of Audio. Doing so, even if you are muted (on the IPhone, I Have not tested this with any external device) you will be able to record your audio... here is the piece of code that does that:
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryRecord
error: &error];
it even records with a higher volume.
Best of luck...

AVAudioSession category not working as documentation dictates

I have an iOS app that has some audio feedback in certain places, but I want any other music the user has playing in the background to be allowed to play over this. In addition, I want the audio in my app to respect the mute switch. According to the developer documentation, this functionality should all be enabled by the AVAudioSession ambient category. This is the code I'm using:
if (!hasInitialisedAudioSession) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryAmbient error:NULL];
[session setActive:YES error:NULL];
hasInitialisedAudioSession = YES;
}
The code is executing just fine, and it does indeed let the app sounds play over iPod music. What it doesn't do, however, is respect the mute switch. I've tried swapping this code out for similar C audio calls (stuff like AudioSessionSetProperty) instead of the Objective-C calls, but I get the same result - the ambient session category simply doesn't want to respect the mute switch, despite what the documentation says it should be doing.
Any ideas? Thanks for the help :)
I think I managed to work it out - turns out that it has nothing to do with my app at all, but rather the iPod app. My app obeys the mute switch as it should when the iPod isn't playing, and then allows the iPod to play over it - all behaviour I wanted. However, when the iPod is playing, the app stops responding to the mute switch, so I think it's just something the iPod does to the device audio settings. I could probably work a way around it if I really wanted to spend the time on it, but as long as it obeys the mute switch when the iPod isn't playing that's good enough for me.
EDIT: to work around this, just use this function to determine whether or not the mute switch is on manually, and don't play your sounds if the result is YES. Could be a bit of a pain if you don't have a central audio manager class, though. It would be nice if Apple could publish this behaviour in their documentation.
- (BOOL)deviceIsSilenced
{
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
return (CFStringGetLength(state) <= 0);
}