iOS后台播放h5音频

1、引入头文件

#import 

2、在Appdelegate中找到

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

在中间加入

NSError*setCategoryErr =nil;

NSError*activationErr  =nil;

[[AVAudioSessionsharedInstance] setCategory:AVAudioSessionCategoryPlayback error: &setCategoryErr];

[[AVAudioSessionsharedInstance] setActive:YES error: &activationErr];

3、在Appdelegate中找到

- (void)applicationDidEnterBackground:(UIApplication*)application

加入

UIApplication*  app = [UIApplicationsharedApplication];

__blockUIBackgroundTaskIdentifierbgTask;

bgTask = [appbeginBackgroundTaskWithExpirationHandler:^{

dispatch_async(dispatch_get_main_queue(), ^{

if(bgTask !=UIBackgroundTaskInvalid){

bgTask =UIBackgroundTaskInvalid;

}

});

}];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

dispatch_async(dispatch_get_main_queue(), ^{

if(bgTask !=UIBackgroundTaskInvalid){

bgTask =UIBackgroundTaskInvalid;

}

});

});

你可能感兴趣的:(iOS后台播放h5音频)