swift – 如何使用音频反馈在WatchOS上构建Workout应用程序?

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

有人可以查看我的快速代码并帮助我弄清楚我错过了什么吗?

这是我的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)

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...