我正在構建一個應用程式,它會產生聲音并需要監控它們的音量。我正在嘗試使用 HealthKit,這是我迄今為止所做的:
private let healthStore = HKHealthStore()
func observeHeadphoneAudio() {
guard let type = HKObjectType.categoryType(forIdentifier: .headphoneAudioExposureEvent) else {
return
}
healthStore.requestAuthorization(toShare: nil, read: [type]) { [weak self] success, error in
if let error = error {
log.warning(error.localizedDescription)
} else if success {
self?.fetch(type: type)
}
}
}
func fetch(type: HKCategoryType) {
let descriptor = HKQueryDescriptor(sampleType: type, predicate: nil)
let query = HKObserverQuery(queryDescriptors: [descriptor]) { query, samples, completion, error in
if let error = error {
log.warning(error.localizedDescription)
}
if let samples = samples {
log.info(samples)
}
}
healthStore.execute(query)
}
我在生成聲音時啟動它,使用空樣本集執行一次查詢完成,就是這樣。
監聽耳機音頻曝光的正確方法是什么?
uj5u.com熱心網友回復:
您是在尋找耳機音頻曝光還是耳機音頻曝光事件?當您超過耳機音頻暴露限制時會生成后者。看
https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifier/3081272-headphoneaudioexposure/ https://developer.apple.com/documentation/healthkit/hkcategorytypeidentifier/3552059-headphoneaudioexposureevent
uj5u.com熱心網友回復:
我將事件型別更改為headphoneAudioExposure并創建了如下內容:
private let healthStore = HKHealthStore()
func start() {
authorize { [weak self] type in
self?.runQuery(for: type)
}
}
func authorize(successHandler: @escaping (HKQuantityType) -> Void ) {
guard let type = HKObjectType.quantityType(forIdentifier: .headphoneAudioExposure) else {
return
}
healthStore.requestAuthorization(toShare: nil, read: [type]) { success, error in
if let error = error {
log.warning(error.localizedDescription)
} else if success {
successHandler(type)
} else {
log.warning("Unknown error")
}
}
}
func runQuery(for type: HKQuantityType) {
let startDate = Date()
let predicate = HKQuery.predicateForSamples(withStart: startDate,
end: .distantFuture,
options: .strictStartDate)
let interval = DateComponents(nanosecond: 500_000_000)
let query = HKStatisticsCollectionQuery(quantityType: type,
quantitySamplePredicate: predicate,
options: .mostRecent,
anchorDate: startDate,
intervalComponents: interval)
query.initialResultsHandler = { _, statisticsCollection, error in
log.info("----------INITIAL-----------")
if let error = error {
log.warning(error.localizedDescription)
} else if let lastStatistics = statisticsCollection?.statistics().last {
log.info(lastStatistics.startDate)
log.info(lastStatistics.endDate)
log.info(lastStatistics.mostRecentQuantity())
} else {
log.warning("No statistics yet")
}
log.info("----------INITIAL-----------")
}
query.statisticsUpdateHandler = { _, statistics, _, error in
if let error = error {
log.warning(error.localizedDescription)
} else if let statistics = statistics {
log.info("---------UPDATE------------")
log.info(statistics.startDate)
log.info(statistics.endDate)
log.info(statistics.mostRecentQuantity())
log.info(statistics.quantityType)
}
}
healthStore.execute(query)
}
這段代碼的問題是它有時作業有時不作業,我不知道為什么。此外,當我能夠獲取一些資料時,statisticsUpdateHandler它們每 60-70 秒就會以資料包的形式出現。任何建議,我做錯了什么?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/339847.html
上一篇:只允許在一定時間后觸摸
