-1

I am getting "Use of unresolved identifier 'player' in my code using beacons and regions. For this particular region, I also want it to play a sound (Siren.wav). Code is below:

import Combine
import CoreLocation
import SwiftUI
import AVFoundation

class BeaconDetector: NSObject, ObservableObject, CLLocationManagerDelegate {
    var objectWillChange =  ObservableObjectPublisher()
    var locationManager: CLLocationManager?
    var lastDistance = CLProximity.unknown
    var player: AVAudioPlayer?
    //   var audioPlayer = AVAudioPlayer()


    override init() {
        super.init()

        locationManager = CLLocationManager()
        locationManager?.delegate = self
        locationManager?.requestWhenInUseAuthorization()
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {
                if CLLocationManager.isRangingAvailable() {
               startScanning()
                }
            }
        }
    }

    func startScanning() {
        let uuid = UUID(uuidString: "00000000-0000-0000-0000-000000000000")!
        let constraint = CLBeaconIdentityConstraint(uuid: uuid)
        let beaconRegion = CLBeaconRegion(beaconIdentityConstraint: constraint, identifier: "MyBeacon")

        locationManager?.startMonitoring(for: beaconRegion)
        locationManager?.startRangingBeacons(satisfying: constraint)
    }

    func locationManager(_ manager: CLLocationManager, didRange beacons: [CLBeacon], satisfying beaconConstraint: CLBeaconIdentityConstraint) {
        if let beacon = beacons.first {
            update(distance: beacon.proximity)
        } else {
            update(distance: .unknown)
        }
    }

    func update(distance: CLProximity) {
        lastDistance = distance
        self.objectWillChange.send()
    }
}

struct BigText: ViewModifier {
    func body(content: Content) -> some View {
        content
                   .font(Font.system(size: 72, design: .rounded))
         .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
    }
}

struct ContentView: View {
    @ObservedObject var detector = BeaconDetector()

    var body: some View {
        if detector.lastDistance == .immediate {
            return Text("DANGER TOO CLOSE")
            .modifier(BigText())
                .background(Color.red)
                .edgesIgnoringSafeArea(.all)
            func playSound() {
                guard let url = Bundle.main.url(forResource: "Siren", withExtension: "wav") else { return }
                do {
                    try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
                    try AVAudioSession.sharedInstance().setActive(true)

                    player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.wav.rawValue)
                    guard let player = player else { return }

                    player.play()

                }
                catch let error {
                    print(error.localizedDescription)
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
rob
  • 21
  • 2

1 Answers1

0

The reason you get an "unresolved identifier" error is because the variable player is not defined in the playSound() method. In the Swift language, each variable declaration has a specific "scope" and they cannot be accessed outside that scope.

In this case, player is defined as a member variable in the BeaconDetector class. Because the playSound() method is not in the same variable "scope", you get that error when you try to access the variable.

You might want to read this tutorial on how variable scope works in Swift.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204