2017-04-03 31 views
3

我正在WatchOS上構建一個非常簡單的鍛鍊應用程序:其中一項功能是在培訓期間提供音頻反饋。當顯示器打開時,我可以播放文件,但是當顯示屏黑屏時,手錶不會播放我的文件。如何使用音頻反饋在WatchOS上構建鍛鍊應用程序?

有人可以看看我的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() 
    } 
} 

回答

3

我想通了,我做錯了什麼: 對於聲音一起玩關閉屏幕,將類別設置爲AVAudioSessionCategoryPlayback而非環境非常重要。

所以這就是我所做的得到它的工作:

 try audioSession.setCategory(AVAudioSessionCategoryPlayback, with: .duckOthers) 
:我在extensionDelegate.swift的第10行改變了一個字