在我的Swift 2.0应用程序中,我使用CoreLocation检查用户是否在某个地方的1公里范围内,如果是,我使用AVFoundation播放声音。
问题是,当我开始在didUpdateLocations中播放音频时,即使在音频停止后它也会停止更新。
这是我的代码:
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
}
}
}
由于didUpdateLocations刚停止更新,因此当用户进入另一个区域时,我无法播放音频。非常感谢您的帮助!
由于
答案 0 :(得分:1)
看起来你的while循环会在player.rate!= 0.0或者#34;如果让url&#34;条件是错误的,因为i值不会发生变化。我可能错了,但基于查看此页面上的代码,情况似乎就是这样。