gpt4 book ai didi

swift - 如何在 WatchOS 上构建带有音频反馈的锻炼应用程序?

转载 作者:搜寻专家 更新时间:2023-10-30 23:09:03 24 4
gpt4 key购买 nike

我正在 WatchOS 上构建一个非常简单的锻炼应用程序:其中一个功能是在训练期间提供音频反馈。我可以在显示屏打开时播放文件,但是当显示屏变暗时, watch 不播放我的文件。

有人可以查看我的 swift 代码并帮助我弄清楚我缺少什么吗?

这是我的 extensionDelegate.swift:

var audioPlayer = AVAudioPlayer()

class ExtensionDelegate: NSObject, WKExtensionDelegate {
func applicationDidFinishLaunching() {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryAmbient, with: .duckOthers)
} catch {
print("audiosession cannot be set")
}
do { try audioSession.setActive(true) }
catch {
print ("audiosession cannot be activated")
}

let test = URL(fileURLWithPath: Bundle.main.path(forResource: "1", ofType: "m4a")!)
try! audioPlayer = AVAudioPlayer(contentsOf: test)
audioPlayer.prepareToPlay()
audioPlayer.play()
}

func applicationDidBecomeActive() {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print ("shared Instance could not be activated")
}
}

func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
// Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.
for task : WKRefreshBackgroundTask in backgroundTasks {
// Check the Class of each task to decide how to process it
print ("received background tasks")
if task is WKApplicationRefreshBackgroundTask {
// Be sure to complete the background task once you’re done.
let backgroundTask : WKApplicationRefreshBackgroundTask = task as! WKApplicationRefreshBackgroundTask
backgroundTask.setTaskCompleted()
} else if task is WKSnapshotRefreshBackgroundTask {
// Snapshot tasks have a unique completion call, make sure to set your expiration date
let backgroundTask : WKSnapshotRefreshBackgroundTask = task as! WKSnapshotRefreshBackgroundTask
backgroundTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: .distantFuture, userInfo: nil)
} else if task is WKWatchConnectivityRefreshBackgroundTask {
// Be sure to complete the background task once you’re done.
let backgroundTask : WKWatchConnectivityRefreshBackgroundTask = task as! WKWatchConnectivityRefreshBackgroundTask
backgroundTask.setTaskCompleted()
} else if task is WKURLSessionRefreshBackgroundTask {
// Be sure to complete the background task once you’re done.
let backgroundTask : WKURLSessionRefreshBackgroundTask = task as! WKURLSessionRefreshBackgroundTask
backgroundTask.setTaskCompleted()
} else {
// make sure to complete unhandled task types
task.setTaskCompleted()
}
}
}
}

在 InterfaceController.swift 中我调用函数:

func startTimer() {
// every 30 seconds
print ("timer function started")
timer = Timer.scheduledTimer(withTimeInterval: 30.0, repeats: true) { [weak self] _ in
print("play")
audioPlayer.play()
}
}

最佳答案

我发现我做错了什么:对于屏幕关闭时播放的声音,将类别设置为 AVAudioSessionCategoryPlayback 非常重要,而不是环境。

这就是我为让它工作所做的一切:我在 extensionDelegate.swift 的第 10 行更改了一个词:

        try audioSession.setCategory(AVAudioSessionCategoryPlayback, with: .duckOthers)

关于swift - 如何在 WatchOS 上构建带有音频反馈的锻炼应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43190240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com