如何在后台播放音乐

- (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


发布了149 篇原创文章 · 获赞 39 · 访问量 16万+

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览