我在我的应用程序中使用“位置更新的应用程序注册”后台模式来从后台获取位置信息。我的要求是,如果用户离预定位置更近(约100米),则需要播放音频文件。当应用程序处于 Activity 状态(前景)但未从后台播放任何音频时,此方法工作正常。
self.locationManager = [[CLLocationManager alloc]init ];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
// delegate method
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocationDistance distance = [newLocation distanceFromLocation:definedLocation];
//Check the distance between the newLocation and userDefinedLOcation
if (distance <=100.00) {
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.wav", [[NSBundle mainBundle] resourcePath],[[NSUserDefaults standardUserDefaults]valueForKey:@"alarmSound"]]];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
player.numberOfLoops = -1;
player.currentTime = 0;
player.volume =1;
player.delegate=self;
[player prepareToPlay];
[player play];
}
}
如何使用“用于位置更新的应用程序注册”后台模式在后台播放音频文件,帮助我实现这一目标。我的应用被拒绝了,因为我之前使用过“应用播放音频”背景模式。所以我不想使用音频背景模式。
任何帮助都适用。
最佳答案
您是否明确设置了音频 session 类别?
《音频 session 编程指南》有一个有用的部分“选择最佳类别”,它描述了不同的选项:
Audio Session Programming Guide
我现在不在Mac面前,因此无法测试。但是我怀疑将以下内容添加到您的viewDidLoad方法中可能会在您从应用程序执行多任务时帮助您。
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
关于ios - 我如何使用 “App registers for location updates”背景模式在后台播放音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17545619/