【问题标题】:didUpdateLocations stops updatingdidUpdateLocations 停止更新
【发布时间】:2016-09-12 18:41:36
【问题描述】:

在我的 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”条件为假会发生什么?
  • @DevKyle 什么都不应该发生。只有当播放器的速率为 0.0(即它不播放任何内容)并且 url 有效时,才需要播放音频。我觉得奇怪的是,当音频开始播放时,CoreLocation 似乎完全停止工作。
  • 您是否使用 'requestLocation()` 方法获取您的位置?这导致Core Location获取设备位置,然后停止更新设备位置
  • @Adolfo 我使用 manager.startUpdatingLocations() 获取位置。当我注释掉音频部分时,它可以正常工作并且每秒更新一次,但是当我取消注释它时,它只会被调用一次或两次。
  • 我问了这个问题,因为如果不满足条件,您的 while 循环将被卡住,因为 i 值不会改变。我可能错了,但根据查看此页面上的代码,情况似乎如此。不确定这是否有帮助..

标签: swift swift2 avfoundation core-location


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    相关资源
    最近更新 更多