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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了试图在Swift的信标区域内合并声音相关的知识,希望对你有一定的参考价值。

我在代码中使用信标和区域获得了“使用未解析的标识符'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)
答案

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

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

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

以上是关于试图在Swift的信标区域内合并声音的主要内容,如果未能解决你的问题,请参考以下文章

当 UIImageView 在特定区域内时播放声音

Swift:无法根据数组播放每个声音序列

Swift - 检查 url 资产是不是有声音

iOS Swift:声音没有播放

如何让背景音乐继续播放而我的应用程序在使用 Swift 时仍然播放其声音

使用媒体播放器或声音池在片段内的 onClick 中播放声音