This commit is contained in:
Trey t
2023-08-12 13:41:53 -05:00
parent 4824fbc0fe
commit 2aa83f12f2
6 changed files with 89 additions and 6 deletions

View File

@@ -54,6 +54,7 @@ class BridgeModule: NSObject, ObservableObject {
public private(set) var heartRates: [Int]?
var audioPlayer: AVAudioPlayer?
var avPlayer: AVPlayer?
func start(workout: Workout) {
currentExerciseInfo.complete = {
@@ -130,11 +131,23 @@ class BridgeModule: NSObject, ObservableObject {
}
@objc func updateCurrentExerciseTimer() {
if currentExerciseTimeLeft > 0 {
if currentExerciseTimeLeft > 1 {
currentExerciseTimeLeft -= 1
if currentExerciseTimeLeft <= 3 {
playBeep()
if let currentExercise = currentExerciseInfo.allSupersetExecercise, let audioQueues = currentExercise.audioQueues {
if let audioQueue = audioQueues.first(where: {
$0.playAt == currentExerciseTimeLeft
}) {
switch audioQueue.audioType {
case .shortBeep:
playBeep()
case .finishBeep:
playFinished()
case .remoteURL(let url):
playRemoteAudio(fromURL: url)
}
}
}
sendCurrentExerciseToWatch()
} else {
@@ -154,7 +167,6 @@ class BridgeModule: NSObject, ObservableObject {
func nextExercise() {
if let nextSupersetExercise = currentExerciseInfo.nextExercise {
updateCurrent(exercise: nextSupersetExercise)
playFinished()
} else {
completeWorkout()
}
@@ -205,6 +217,23 @@ class BridgeModule: NSObject, ObservableObject {
}
}
func playRemoteAudio(fromURL url: URL) {
#if os(iOS)
let playerItem = AVPlayerItem(url: url)
do {
try AVAudioSession.sharedInstance().setCategory(.playback,
mode: .default,
options: [.mixWithOthers])
try AVAudioSession.sharedInstance().setActive(true)
avPlayer = AVPlayer(playerItem: playerItem)
avPlayer?.play()
} catch {
print("ERROR")
}
#endif
}
func playBeep() {
#if os(iOS)
if let path = Bundle.main.path(forResource: "short_beep", ofType: "m4a") {
@@ -283,6 +312,7 @@ extension BridgeModule: WCSessionDelegate {
switch model {
case .nextExercise:
nextExercise()
playFinished()
case .workoutComplete(let data):
let model = try! JSONDecoder().decode(WatchFinishWorkoutModel.self, from: data)
totalCaloire = Float(model.totalBurnedEnergery)