如何在后台播放音乐

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

audioSession = [AVAudioSession sharedInstance];

NSError *err = nil;

UIDevice *device = [UIDevice currentDevice];

    BOOL backgroundSupported = NO;

if ([device respondsToSelector:@selector(isMultitaskingSupported)]) {

backgroundSupported = device.multitaskingSupported;

}

if (backgroundSupported) {

NSLog(@"Supports multitasking");

[audioSession setCategory :AVAudioSessionCategoryPlayback error:&err];

}

else {

NSLog(@"Does not support multitasking");

[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];

}

if(err){

NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);

}

[audioSession setActive:YES error:&err];

err = nil;

if(err){

NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);

}

    // Override point for customization after application launch.

    [self.window makeKeyAndVisible];


return YES;

}

 

官方文档:

http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html


你可能感兴趣的:(音乐)