Bobj-C
Bobj-C

Reputation: 5426

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.

Upvotes: 1

Views: 482

Answers (2)

Jason Coco
Jason Coco

Reputation: 78363

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

Upvotes: 4

hotpaw2
hotpaw2

Reputation: 70703

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.

Upvotes: 2

Related Questions