audio - 试图在 Swift 的信标区域中加入声音
问题描述
我正在使用信标和区域在我的代码中使用未解析的标识符'播放器'。对于这个特定区域,我还希望它播放声音(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()
方法不在同一个变量“范围”中,所以当您尝试访问该变量时会出现该错误。
你可能想阅读这篇关于变量作用域如何在 Swift 中工作的教程。
推荐阅读
- java - intelij-idea如何让maven项目在本地工作?
- r - R闪亮的数据表在开始时不显示记录(行)
- rest - swagger 文档中的查询字符串参数未在请求中作为数组发送
- python - 限制其他用户加入电报机器人
- java - 如何在 Hibernate 和 Oracle 中使用命名查询将字符串搜索为数字?
- c++ - 将两个部分相同的图像合并为一张图像
- selenium - 如何使用 Selenium 和 Scrapy 从 csv 抓取多个 URL
- sf - 如何从 SpatialLinesDataFrame 创建 SpatialpolygonsDataFrame
- python - 获取用户列表以及他们从数据框中购买了多少
- c# - 生成 Edi 276 文件