Skip to content

Commit

Permalink
refactor: perform player seek async
Browse files Browse the repository at this point in the history
  • Loading branch information
godly-devotion committed Mar 23, 2024
1 parent c3978a3 commit a240c8c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions Front Row/Main Menu/PlaybackCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct PlaybackCommands: Commands {
.disabled(!playEngine.isLoaded)

Button {
playEngine.goToTime(0.0)
Task { await playEngine.goToTime(0.0) }
} label: {
Text(
"Restart",
Expand All @@ -38,7 +38,7 @@ struct PlaybackCommands: Commands {
}
Section {
Button {
playEngine.goForwards()
Task { await playEngine.goForwards() }
} label: {
Text("Go Forward 5s")
}
Expand All @@ -47,7 +47,7 @@ struct PlaybackCommands: Commands {
!playEngine.isLoaded || presentedViewManager.isPresenting)

Button {
playEngine.goBackwards()
Task { await playEngine.goBackwards() }
} label: {
Text("Go Backward 5s")
}
Expand Down
18 changes: 9 additions & 9 deletions Front Row/Support/PlayEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,31 +143,31 @@ import SwiftUI
}
}

func goForwards(_ duration: Double = 5.0) {
func goForwards(_ duration: Double = 5.0) async {
guard isLoaded else { return }
let time = CMTimeAdd(
player.currentTime(),
CMTimeMakeWithSeconds(duration, preferredTimescale: 1)
)
player.seek(to: time, toleranceBefore: CMTime.zero, toleranceAfter: CMTime.zero)
await player.seek(to: time, toleranceBefore: .zero, toleranceAfter: .zero)
}

func goBackwards(_ duration: Double = 5.0) {
func goBackwards(_ duration: Double = 5.0) async {
guard isLoaded else { return }
let time = CMTimeSubtract(
player.currentTime(),
CMTimeMakeWithSeconds(duration, preferredTimescale: 1)
)
player.seek(to: time, toleranceBefore: CMTime.zero, toleranceAfter: CMTime.zero)
await player.seek(to: time, toleranceBefore: .zero, toleranceAfter: .zero)
}

func goToTime(_ timecode: Double) {
func goToTime(_ timecode: Double) async {
guard isLoaded else { return }
let time = CMTimeMakeWithSeconds(timecode, preferredTimescale: 1)
player.seek(to: time, toleranceBefore: CMTime.zero, toleranceAfter: CMTime.zero)
await player.seek(to: time, toleranceBefore: .zero, toleranceAfter: .zero)
}

func goToTime(_ timecode: String) {
func goToTime(_ timecode: String) async {
guard let item = player.currentItem else { return }

let split = Array(timecode.split(separator: ":").reversed())
Expand All @@ -186,9 +186,9 @@ import SwiftUI
let time = CMTimeMakeWithSeconds(
Double(hour * 3600 + minute * 60) + second, preferredTimescale: 1)

let validRange = CMTimeRange(start: CMTime.zero, end: item.duration)
let validRange = CMTimeRange(start: .zero, end: item.duration)
guard validRange.containsTime(time) else { return }
player.seek(to: time, toleranceBefore: CMTime.zero, toleranceAfter: CMTime.zero)
await player.seek(to: time, toleranceBefore: .zero, toleranceAfter: .zero)
}

func fitToVideoSize() {
Expand Down
2 changes: 1 addition & 1 deletion Front Row/Views/GoToTimeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct GoToTimeView: View {
VStack {
TextField(text: $timecode, prompt: Text(verbatim: "0:00:00")) {}
.onSubmit {
PlayEngine.shared.goToTime(timecode)
Task { await PlayEngine.shared.goToTime(timecode) }
dismiss()
}
.autocorrectionDisabled()
Expand Down

0 comments on commit a240c8c

Please sign in to comment.