AVAudioPlayer不会在didEnterRegion中播放

时间:2012-08-31 06:32:52

标签: ios avaudioplayer cllocationmanager

我问这样的问题,因为UIAlertView会关闭。只是不是AVAudioPlayer。这是我的代码:

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

{
    NSLog(@"MapViewController - didEnterRegion");

    LocationReminder *theLocationReminder = [self.locationReminders objectForKey:region.identifier];

    NSError *error;
    NSURL *theUrl = [NSURL URLWithString:theLocationReminder.audioURI];

    NSLog(@"theUrl = %@",theUrl);

    AVAudioPlayer *thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:theUrl error:&error];

    NSLog(@"thePlayer.url = %@",thePlayer.url);

    [thePlayer play];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"entered region..." message:@"You have Entered the Location." delegate:nil cancelButtonTitle:@"OK"  otherButtonTitles: nil];
    alert.tag = 2;
    [alert show];
}

我在测试后检查了设备上的日志,当我正在测试它时,警报视图确实显示,但AVAudioPlayer即使拥有正确的URL也不会播放。

我应该提一下,虽然我已经在需要的背景模式下在plist中启用了音频,但我仍然在应用程序中(因此后台处理不会成为问题)。

1 个答案:

答案 0 :(得分:0)

您没有保留对thePlayer的引用,因此它已被取消分配(假设您正在使用ARC)。可能在类@interface块中添加属性,如

@property (nonatomic, strong) AVAudioPlayer *player;

然后代替你做过的行

self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:theUrl error:&error];

希望这有帮助!