Reputation: 470
In my Swift 2.0 app, I'm using CoreLocation to check whether the user is within 1km of a certain place and if they are, I use AVFoundation to play a sound.
The problem is, when I start playing the audio within the didUpdateLocations, it stops updating, even after the audio stops..
Here's my code:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//Method called every time the location is updated by CoreLocation
let userLocation = locations[0]
var i = 0
while i < coordinates.count {
//Looping through all different coordinates
let location = CLLocation(coordinate: coordinates[i], altitude: 0, horizontalAccuracy: kCLLocationAccuracyBest, verticalAccuracy: kCLLocationAccuracyBest, timestamp: NSDate())
if userLocation.distanceFromLocation(location) < 1001 {
//If the user is within 1000m of area[i]
if player.rate == 0.0 {
//Checks for the player already playing something else
if let url = NSBundle.mainBundle().URLForResource(titles[i], withExtension: "mp3") {
player.removeAllItems()
player.insertItem(AVPlayerItem(URL: url), afterItem: nil)
player.play()
i = 10 //We had an area that was closer and the player is finished, so we stop the loop
}
}
} else {
//This location was further than 1000 meters, so we up the count and check the next coordinate
i += 1
}
}
}
Since the didUpdateLocations just stops updating, I can't play audio when the user enters another area. Your help is highly appreciated!
Thanks
Upvotes: 0
Views: 167
Reputation: 1091
It looks like your while loop will be stuck if player.rate != 0.0, or the "if let url" condition is false because the i-value won't change. I might be wrong, but based on looking at the code that is on this page, that would seem to be the case.
Upvotes: 1