试图在Swift的信标区域内合并声音

问题描述 投票:-1回答:1

我在代码中使用信标和区域获得了“使用未解析的标识符'player'。对于这个特定区域,我也希望它播放声音(Siren.wav)。代码如下:

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)
audio beacon unresolved-external
1个回答
0
投票

得到“未解析的标识符”错误的原因是因为player方法中未定义变量playSound()。在Swift语言中,每个变量声明都有一个特定的“作用域”,并且不能在该范围之外访问它们。

在这种情况下,player被定义为BeaconDetector类中的成员变量。因为playSound()方法不在同一变量“作用域”中,所以当您尝试访问该变量时,会出现该错误。

您可能想读this tutorial关于变量作用域如何在Swift中工作。

© www.soinside.com 2019 - 2024. All rights reserved.