run music in background running application in xcode - iphone

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

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

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)

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

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?

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.

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