audioSession.inputIsAvailable = NO in 2nd Generation iPod Touch 4.2.1 - iphone

I'm trying to record the voice of the user using AVAudioRecorder class.
First of all I set the AudioSession
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error;
if ([audioSession inputIsAvailable]) {
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
}
The value of audioSession.inputIsAvailable is always NO for my 2nd Gen iPod Touch.
Why does it happen? Is not possible to record with this device? Or does it depend on the OS?

Related

ios remote sometimes defaults to native music player instead of my app

About 1 in 20 times my app (after launch) does not seems to 'beginReceivingRemoteControlEvents' and hence the ios remote defaults to the native music player. Because of this my app has been rejected (at an update, not initial release), the thing is I can never see it happen when the iPhone is plugged into mac and xcode only when running the app on the phone, below is my audio initialization routine.
Hope someone can help, thanks Mike.
-(void)viewDidAppear:(BOOL)animated
{
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
[[AVAudioSession sharedInstance] setDelegate: self];
//Set the audio category of this app to playback.
NSError *setCategoryError = nil; [[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError)
{
ShowMessageBox(#"Audio error", #"Category failed");
}
//Activate the audio session
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];
if (activationError) {
ShowMessageBox(#"Audio error", #"Activate failed");
}
}
// And also
- (BOOL)canBecomeFirstResponder
{
return YES;
}

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)

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 ...

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

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