开发者

How to fade any sound when your app wants to start?

Is there any method to开发者_如何学JAVA 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜