views:

89

answers:

2

Is there any method to fade sounds like iPod music when the user want to use your app?

Thanks.

+2  A: 

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.

hotpaw2
+4  A: 

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);
  }
}
Jason Coco
hello thanks for answer i got this warning when i used ur code warning: 'AVAudioSession' may not respond to '-setActive:withError:' and the app no longer run in the simulator exit after 2 second. solution ?
Bobj-C
@Bobj-C: Sorry, I was typing from memory. I'll fix it in the code sample, but the method is setActive:error:.
Jason Coco
Bobj-C
@Bobj-C: Yeah, I typed the method name wrong from memory. The actual method is -setActive:error: and I updated the code sample in my answer to reflect the correct method name.
Jason Coco