我是新手,想把信标和声音结合起来,返回后得到的东西不会被执行

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

我在这段代码中使用了信标,并希望有不同颜色和信息的区域。在离信标最近的区域,我想让屏幕变成红色,显示 "你离我太近了 "的信息,并播放警报声。颜色和信息都可以,但是声音从来没有播放过。这是我的代码,一定是我在 "危险离我太近 "部分使用了let这个词,导致它后面没有执行任何内容,但我该把let改成什么呢?

import Combine
import CoreLocation
import SwiftUI
import AVFoundation

class BeaconDetector: NSObject, ObservableObject, CLLocationManagerDelegate {
    var objectWillChange = ObservableObjectPublisher()
    var locationManager: CLLocationManager?
    var lastDistance = CLProximity.unknown
    var alarm: 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 TO ME")
             .modifier(BigText())
             .background(Color.red)
             .edgesIgnoringSafeArea(.all)

        let sndurl = Bundle.main.url (forResource: "Alarm", withExtension: "mp3")!
        var snd : SystemSoundID = 0
         AudioServicesCreateSystemSoundID(sndurl as CFURL, &snd)
         AudioServicesPlaySystemSoundWithCompletion (snd) { AudioServicesDisposeSystemSoundID(snd) }
        }

        else if detector.lastDistance == .near {
        return Text("Outer Zone")
             .modifier(BigText())
             .background(Color.orange)
             .edgesIgnoringSafeArea(.all)
        } else if detector.lastDistance == .far {
        return Text("Far")
             .modifier(BigText())
             .background(Color.yellow)
             .edgesIgnoringSafeArea(.all)
        } else {
        return Text("UNKNOWN")
            .modifier(BigText())
            .background(Color.green)
            .edgesIgnoringSafeArea(.all)


    }
}

谢谢,谢谢

audio beacon region
1个回答
0
投票

在这里,我想在离信标最近的区域,让屏幕变成红色,有一条你的信息。return 语句导致子程序方法的执行停止。 然后恢复执行,不管是谁调用了该方法,都会恢复执行,而在 return 语句(在这里是一个 Text 对象)被提供给调用者。

要解决你问到的具体问题,只需将播放声音的代码移到你的声音对象上面。return 语句,使其真正执行。

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