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.
Related
I'm learning core audio. For some reason the sound of the processing graph only plays through the weak "ear speakers" (when you hold device to your ear) but not over the regular speakers of the iPhone.
This is the code which sets up the audio session but I can't see where it configures the audio route:
- (void) setupAudioSession {
AVAudioSession *mySession = [AVAudioSession sharedInstance];
// Specify that this object is the delegate of the audio session, so that
// this object's endInterruption method will be invoked when needed.
[mySession setDelegate: self];
// Assign the Playback category to the audio session.
NSError *audioSessionError = nil;
[mySession setCategory: AVAudioSessionCategoryPlayAndRecord//AVAudioSessionCategoryPlayback
error: &audioSessionError];
if (audioSessionError != nil) {
NSLog (#"Error setting audio session category.");
return;
}
// Request the desired hardware sample rate.
self.graphSampleRate = 44100.0; // Hertz
[mySession setPreferredHardwareSampleRate: graphSampleRate
error: &audioSessionError];
if (audioSessionError != nil) {
NSLog (#"Error setting preferred hardware sample rate.");
return;
}
// Activate the audio session
[mySession setActive: YES
error: &audioSessionError];
if (audioSessionError != nil) {
NSLog (#"Error activating audio session during initial setup.");
return;
}
// Obtain the actual hardware sample rate and store it for later use in the audio processing graph.
self.graphSampleRate = [mySession currentHardwareSampleRate];
// Register the audio route change listener callback function with the audio session.
AudioSessionAddPropertyListener (
kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback,
self
);
}
At which point in core audio do you say "play over speakers" when playing sounds with audio units?
You can use the setCategory withOption:
[mySession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&audioSessionError];
I had the same problem. Turns out it is something to do with the "play and record" category. Just need to redirect the audio output.
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (
kAudioSessionProperty_OverrideAudioRoute,
sizeof (audioRouteOverride),
&audioRouteOverride
);
Source:
http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Cookbook/Cookbook.html#//apple_ref/doc/uid/TP40007875-CH6-SW35
I'm writing a radio app for both iPhone and iPad and am running into some weird behaviour when handling pausing and playing the audio with interruptions. I'm using the AVAudioSessionDelegate methods beginInterruption and endInterruption to respectively pause and play the AVPlayer. Below is the relevant play code.
Now, the following situations seem to happen consistently:
On iPad: If I force an interruption (Facetime call), beginInterruption is called as expected and playback stops. If the interruption stops, endInterruption is called as expected, and the playback resumes as expected.
On iPhone: Pressing the play and pause button, triggers pause and play exactly the same as beginInterruption and endInterruption would. Playback behaves as expected.
On iPhone: Forcing an interruption (by calling the phone), calls endInterruption as expected and playback pauses as expected. However, when the interruption is finished, beginInterruption is called as expected, play is called as expected, it actually reaches and executes the [self.player play] line, but playback does not resume! I hear nothing.
Situation 3 above is extremely weird, so I'm wondering if I may have overlooked something. Any ideas?
Play code
- (void)play {
NSError* error = nil;
AVKeyValueStatus keyStatus = [currentAsset statusOfValueForKey:#"tracks" error:&error];
if(error){
DLog(#"Error %#", [error localizedDescription]);
}
else {
DLog(#"Current Key Status: %i", keyStatus);
if(keyStatus == AVKeyValueStatusLoaded){
DLog(#"Continue playing source: %#", self.source);
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
if (error) {
DLog(#"Error during play while setting AVAudioSessionCategory: %#", [error localizedDescription]);
}
[[AVAudioSession sharedInstance] setActive:YES error:&error];
if (error) {
DLog(#"Error during play while setting AVAudioSessionCategory: %#", [error localizedDescription]);
}
[[AVAudioSession sharedInstance] setDelegate:self];
if(backgroundTaskID != UIBackgroundTaskInvalid){
[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskID];
}
backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
[self.player play];
[self setStatus:kNPOMediaPlayerStatusPlaying];
}
else {
DLog(#"PlaySource: %#", self.source);
[self playSource:self.source];
}
}}
I had the same issue. Issue disappeared after I implemented remote control event handling. I called [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];when starting playback.
It turns out that this is a known bug in iOS, which requires some careful work-arounds in the applicationDidBecomeActive to handle beginInterruption. Sadly, I couldn't figure out another solution.
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)
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 ...
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