In meiner Swift 2.0 App verwende ich CoreLocation, um zu überprüfen, ob sich der Benutzer innerhalb eines Kilometer von einem bestimmten Ort befindet. Wenn ja, benutze ich AVFoundation, um einen Ton abzuspielen.didUpdateLocations stoppt die Aktualisierung
Das Problem ist, wenn ich innerhalb der didUpdateLocations spiele das Audio starten, Aktualisierung stoppt, selbst nachdem das Audio stoppt ..
Hier ist mein 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
}
}
}
Da die didUpdateLocations nicht mehr nur die Aktualisierung , Ich kann keine Audiowiedergabe durchführen, wenn der Benutzer einen anderen Bereich betritt. Deine Hilfe wird hoch geschätzt!
Dank
Was passiert, wenn player.rate! = 0.0, oder die Bedingung "if let url" falsch ist? – DevKyle
@DevKyle Nichts sollte passieren. Nur wenn die Rate des Players 0.0 ist (d. H. Es wird nichts abgespielt) und die URL ist gültig, muss das Audio abgespielt werden. Was ich seltsam finde, ist, dass CoreLocation, wenn der Ton abgespielt wird, überhaupt nicht mehr funktioniert. –
Erhalten Sie Ihren Standort mit der Methode 'requestLocation() '? Dies führt dazu, dass Core Location Gerätestandort erhält und dann zum Aktualisieren des Gerätestandorts stoppt. – Adolfo