在后台播放语音(推送语音播报)

1.在TARGETS -->Capabilities–>Background Modes开启并勾选Audio,AirPlay,and Picture

2.在- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions中写入以下代码:

NSError*setCategoryErr =nil;

NSError*activationErr  =nil;

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr];

[[AVAudioSession sharedInstance] setActive: YES error: &activationErr];

3.在- (void)applicationDidEnterBackground:(UIApplication*)application中写入以下代码:

UIApplication*  app = [UIApplication sharedApplication];

__block    UIBackgroundTaskIdentifier bgTask;

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)