2017-04-03 1 views
3

나는 WatchOS에서 아주 간단한 운동 프로그램을 만들고있다 : 그 중 하나는 훈련 중에 오디오 피드백을주는 것이다. 디스플레이가 켜져있을 때 파일을 재생할 수 있지만 디스플레이가 어두울 때 시계가 내 파일을 재생하지 않습니다.Audio Feedback으로 WatchOS에서 운동 앱을 만드는 방법은 무엇입니까?

일부 사용자는 내 신속한 코드를 살펴보고 내가 누락 된 항목을 파악하는 데 도움이 될 수 있습니까? 내가 함수를 호출

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에서

: 여기

내 extensionDelegate.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 행에 한 단어를 변경
관련 문제