IPhone SDK: Audio Session Error: -12986 .... after upgrade to 3.1 - iphone

I am building an iPhone audio app using Audio Sessions. Prototype was functioning till I decided to upgrade to 3.1
After a lot of hunting I finally found that the session activation call was failing with error code 12986.
I havent been able to find the reason for this anywhere.
The NSError object doesnt give any detail. I used the localized* APIs to get more info and this is what I got:
localizedDescription: Operation could not be completed. (OSStatus error -12986.)
localizedFailureReason: <blank>
localizedRecoverySuggestion: <blank>
Anyone know how to find more info about such error codes?
Meanwhile I will continue to dig and update this if my status changes.
My Code for the curious is -
NSError *myErr;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&myErr];
bSuccess= [audioSession setActive: YES error: &myErr];

Dont know what 12986 means exactly but it appears to be tied to the audio capabilities of the device now. And I have a solution!
I noticed that this error was popping up only when I use an iTouch and not on the IPhone. Since I was setting the session category as PlayAndRecord on both I decided to check if that was messing it up on the iTouch. Made the code a little smarter to detect if AudioInputIsAvailable and then set the Category accordingly (PlayBack on ITouch and PlayAndRecord on iPhone). That fixed it!
So it looks like this was being ignored in the prior SDKs. I had not changed anything earlier. :-)
Corrected Code Below:
NSError *myErr;
BOOL bSuccess = FALSE;
BOOL bAudioInputAvailable = FALSE;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
bAudioInputAvailable= [audioSession inputIsAvailable];
if( bAudioInputAvailable)
{
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&myErr];
}
else {
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&myErr];
}
bSuccess= [audioSession setActive: YES error: &myErr];
if(!bSuccess)
{
NSLog(#"Unable to Start Audio Session. Terminate Application.");
NSLog([myErr localizedDescription]);
NSLog([myErr localizedFailureReason]);
NSLog([myErr localizedRecoverySuggestion]);
}

I've had similar trouble trying to extract useful information from the error object as well when doing core data operations, i found the following code to be helpful in determining more precisely the cause of an error.
NSError *error;
... your code here ...
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors != nil && [detailedErrors count] > 0)
{
for(NSError* detailedError in detailedErrors)
{
NSLog(#" DetailedError: %#", [detailedError userInfo]);
}
}
else
{
NSLog(#" %#", [error userInfo]);
}
Sorry i couldn't help you out with your audio problem.
HTH

Related

kAudioSessionProperty_OtherAudioIsPlaying Always Returns True

I have followed as many different examples as I can, most of them in the Apple documentation, but I cannot find out the reason why kAudioSessionProperty_OtherAudioIsPlaying always returns a positive on my devices.
I have initialized and activated the session, yet it still returns positives when my music is paused.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
AVAudioSession* session = [AVAudioSession sharedInstance];
NSError *activationError = nil;
BOOL success = [session setActive: YES error: &activationError];
if (!success) { NSLog(#"%#", activationError); }
UInt32 otherAudioIsPlaying;
UInt32 propertySize = sizeof (otherAudioIsPlaying);
AudioSessionGetProperty (
kAudioSessionProperty_OtherAudioIsPlaying,
&propertySize,
&otherAudioIsPlaying
);
if (otherAudioIsPlaying) {
[session setCategory: AVAudioSessionCategoryAmbient error: nil];
} else {
[session setCategory: AVAudioSessionCategorySoloAmbient error: nil];
}
}
What am I doing wrong?
I had the same problem, managed to fix it by ensuring that an AVAudiosession has been instantiated BEFORE checking the property as follows:
AVAudioSession *session = [AVAudioSession sharedInstance];
Make sure that this occurs before you attempt to check before any other audio is playing and it should work fine :)

How to Play a sound using AVAudioPlayer when in Silent Mode in iPhone

I want to play a sound even in silent mode in iPhone.
Can it be done by using AVAudioPlayer (Without using AVAudioSession)
(For ios 3.0+)
Thanks in advance.
Actually, you can do this. It is controlled via the Audio Session and has nothing to do with AVAudioPlayer itself. Why don't you want to use AudioSession? They play nice together...
In your app, you should initialize the Audio Session, and then you can also tell indicate what kind of audio you intend to play. If you're a music player, then it sort of makes sense that the user would want to hear the audio even with the ring/silent switch enabled.
AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);
// Allow playback even if Ring/Silent switch is on mute
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,
sizeof(sessionCategory),&sessionCategory);
I have an app that I do this very thing, and use AVAudioPlayer to play audio, and with the ring/silent switch enabled, I can hear the audio.
UPDATE (11/6/2013)
In the app I mentioned above, where I used the code above successfully, I have (for some time) been using the following code instead to achieve the same result:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error = nil;
BOOL result = NO;
if ([audioSession respondsToSelector:#selector(setActive:withOptions:error:)]) {
result = [audioSession setActive:YES withOptions:0 error:&error]; // iOS6+
} else {
[audioSession setActive:YES withFlags:0 error:&error]; // iOS5 and below
}
if (!result && error) {
// deal with the error
}
error = nil;
result = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];
if (!result && error) {
// deal with the error
}
I thought I'd post this as an alternative, in light of the most recent comment to this answer. :-)
MarkGranoff's solution is correct. However, if you prefer to do it in Obj-c instead of C, the following works as well:
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
The above answers are correct. Following is the Swift version.
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
//print("AVAudioSession Category Playback OK")
do {
try AVAudioSession.sharedInstance().setActive(true)
//print("AVAudioSession is Active")
} catch _ as NSError {
//print(error.localizedDescription)
}
} catch _ as NSError {
//print(error.localizedDescription)
}
Swift 4 simple version:
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
This will simply do the trick (using AVAudioSession)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)

this app was developed and works fine under ios 5.0, but crashes under ios 4.3

I developed an iPhone app under iOS 5.0, and it works fine. But when it comes to iOS 4.3(Base SDK = latest iOS 5.0, compiler = Apple LLVM 3.0, Deployment Target = iOS 4.3), it crashes after launching.
The output around crash point looks like:
2011-12-06 16:25:08.177 FMWei[466:c203] -[AVAudioSession setMode:error:]: unrecognized selector sent to instance 0x706a7f0
2011-12-06 16:25:08.181 FMWei[466:c203] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AVAudioSession setMode:error:]: unrecognized selector sent to instance 0x706a7f0'
It looks like that AVAudioSession doesn't have a member function setMode:error: while I invoked it. But what's strange is that I didn't invoke a function whose name is setMode:error:. The code about audio processing is:
audio_session = [[AVAudioSession sharedInstance] retain];
audio_session_err = nil;
[audio_session setCategory: AVAudioSessionCategoryPlayAndRecord error:&audio_session_err];
NSLog(#"!");
UInt32 audioRouteOverride = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,sizeof (audioRouteOverride),&audioRouteOverride);
UInt32 allowMixing = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);
if (audio_session_err)
{
NSLog(#"audioSession: %# %d %#", [audio_session_err domain], [audio_session_err code], [audio_session_err description]);
}
else
{
audio_session_err = nil;
[audio_session setActive:YES error:&audio_session_err];
if (!audio_session_err) NSLog(#"audio session is activated successfully");
}
Please help me figure out why it crashes under iOS 4.3 with the strange error. Thank you!
At runtime, lots of methods are called that are not in your code, but which are called behind the scenes as a result of the API calls you have made.
I would focus not on the method that is being called, but on why the object it is sent to is unable to respond to the selector. The object could have been cast as the wrong type, and so is not inheriting the right methods. (In the code snippet you show, you don't explicitly cast AVAudioSession *audio_session.) The other direction is to check that you're not using some other API call that is iOS 5 only, which in the background is calling this method and thus generating the error.
Finally, if you're only recently changed your build target to include iOS 4.3, you may simply need to do a clean build (Product > Clean) so that it compiles iOS 4.3-compatible code.
Maybe you can try the code snippet below
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
NSError * audio_session_err = nil;
[audio_session setCategory: AVAudioSessionCategoryPlayAndRecord error:&audio_session_err];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&audio_session_err];
[[AVAudioSession sharedInstance] setDelegate:self];
NSLog(#"!");
UInt32 audioRouteOverride = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,sizeof (audioRouteOverride),&audioRouteOverride);
UInt32 allowMixing = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);
if (audio_session_err) {
NSLog(#"audioSession: %# %d %#", [audio_session_err domain], [audio_session_err code], [audio_session_err description]);
} else {
audio_session_err = nil;
[[AVAudioSession sharedInstance] setActive:YES error:&audio_session_err];
if (!audio_session_err) NSLog(#"audio session is activated successfully");
}
I think audio_session = [[AVAudioSession sharedInstance] retain]; dispatchs the method setMode: by default. And the setMode: is only available in iOS 5.0 and later(refer to the Doc).
Or you can try to comment out the code:
UInt32 audioRouteOverride = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,sizeof (audioRouteOverride),&audioRouteOverride);
UInt32 allowMixing = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);
There must be a method dispatches the setMode: by default. Try yourself. :p
Try info malloc 0x706a7f0 in your gdb to get the object that the selector was sent to. Note, the 0x706a7f0 is the address that shown in your crash output as the one in your first code snippet.
And another tip, you might do make clean(Poduct->Clean) and rebuild it.
The AVAudioSession method
- (BOOL)setMode:(NSString *)theMode error:(NSError **)outError
Is marked in the documentation as being available only for iOS 5 and later. In fact given the recent addition of modes to the documentation, it looks like audio session modes are not available at all prior to iOS 5.

run music in background running application in xcode

Kindly send me a code sample of how to run the music file when the application goes in the background. [iphone 4.1].
I don't want the music to stop when my application goes in the background just like the itunes music.
Best Regards,
Naveed Butt
Add "audio" to UIBackgroundModes in your PLIST.
For playback to continue when the phone locks;
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (setCategoryError) { /* handle the error condition */ }
NSError *activationError = nil;
[audioSession setActive:YES error:&activationError];
if (activationError) { /* handle the error condition */ }
You can see the sample in the book of Erica Sadun - how to play audio file ...

How to fade any sound when your app wants to start?

Is there any method to fade sounds like iPod music when the user want to use your app?
Thanks.
Some quick sample code that will do this (call from somewhere when your app becomes active or launches & don't forget to link to AVFoundation framework):
#import <AVFoundation/AVAudioSession.h>
// ...
- (void)setupAudioSession
{
NSError* error = nil;
AVAudioSession* session = [AVAudioSession sharedInstance];
// see documentation for delegate methods you should handle
[session setDelegate:self];
// This category will duck and cancel background category, but can be configured
// later for mixing if you want (making it pretty versatile); see documentation
// on categories for other options
if( ![session setCategory:AVAudioSessionCategoryPlayback error:&error] ) {
// handle error
NSLog(#"Error setting audio category: %#, %#", error, [error userInfo]);
}
if( ![session setActive:YES error:&error] ) {
// handle error
NSLog(#"Error setting audio session as active: %#", error);
}
}
If you configure and activate certain audio session types where your app will play sounds (see Apple's Audio Session reference), the OS will fade out the sound from any background apps currently using the audio output, so that your app will have the resources available.