之前有人问once但我尝试实施其中一个建议的答案(没有被接受)并且没有运气。
我应该提一下,我已经在pList中设置了正确的背景模式。
基本上,我正在尝试在didEnterRegion中播放声音。这是我的代码:
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"MapViewController - didEnterRegion");
NSLog(@"MVC - didEnterRegion - region.radius = %f", region.radius);
// code to get the url (removed for simplicity)
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
self.regionPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
NSLog(@"self.regionPlayer.url = %@",self.regionPlayer.url);
[self.regionPlayer play];
}
网址很好,因为它会在前台播放。在控制台日志中似乎也没问题:
Sep 12 22:33:47 unknown MLTM[4995] <Warning>: MVC - didEnterRegion - region.radius = 250.000000
Sep 12 22:33:47 unknown MLTM[4995] <Warning>: url = file://localhost/var/mobile/Applications/EFD6A583-5685-4D7C-BF8E-C8CFEA9E0D03/MLTM.app/party%20mix%207.caf
Sep 12 22:33:47 unknown MLTM[4995] <Warning>: self.regionPlayer.url = file://localhost/var/mobile/Applications/EFD6A583-5685-4D7C-BF8E-C8CFEA9E0D03/MLTM.app/party%20mix%207.caf
Sep 12 22:33:48 unknown Console[4179] <Notice>: TestFlight: Team Token is recognized
Sep 12 22:33:48 unknown mediaserverd[44] <Error>: 22:33:48.087 <AudioQueueServer> AudioQueue: Error -12985 from AudioSessionSetClientPlayState(4995)
我假设最后一行是尝试播放音频时给出的错误。
有什么想法吗?
答案 0 :(得分:0)
添加:
register **UIBackgroundModes** in info.plist for background playing
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];