I want to play audio in the background when I quit an application, but the following code does not appear to achieve this. What might I be doing wrong?
- (void)applicationDidEnterBackground:(UIApplication *)application
{
printf("hello");
UIApplication *app = [UIApplication sharedApplication];
//beginBackgroundTaskWithExpirationHandler *bgTask;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{[theAudio play]; [app
endBackgroundTask:bgTask]; bgTask =UIBackgroundTaskInvalid;
NSString *path = [[NSBundle mainBundle] pathForResource:@"Piano"ofType:@"caf"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
});
// Do the work associated with the task.
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;;
NSLog(@"hello%@",bgTask);
}