gpt4 book ai didi

swift - didUpdateLocations 停止更新

转载 作者:行者123 更新时间:2023-11-28 16:11:54 25 4
gpt4 key购买 nike

在我的 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 刚刚停止更新,当用户进入另一个区域时我无法播放音频。非常感谢您的帮助!

谢谢

最佳答案

如果 player.rate != 0.0 或“if let url”条件为假,您的 while 循环似乎会卡住,因为 i 值不会改变。我可能是错的,但根据查看此页面上的代码,情况似乎是这样。

关于swift - didUpdateLocations 停止更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39456732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com