Skip to content

Commit

Permalink
refactor: pause playback before going forwards or backwards to improv…
Browse files Browse the repository at this point in the history
…e seek performance
  • Loading branch information
godly-devotion committed May 9, 2024
1 parent 93b84eb commit 99caae0
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Front Row/Support/PlayEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ import SwiftUI

private(set) var timeRemaining: TimeInterval = 0.0

private var wasPausedBeforeSeeking = false

var playbackSpeed: Float {
get {
access(keyPath: \.playbackSpeed)
Expand Down Expand Up @@ -297,21 +299,31 @@ import SwiftUI
func goForwards() async {
guard isLoaded else { return }

/// If needed pause playback to improve seek performance
pausePlaybackIfNeeded()

let time = CMTimeAdd(
player.currentTime(),
CMTimeMakeWithSeconds(Double(skipInterval), preferredTimescale: 1)
)
await player.seek(to: time, toleranceBefore: .zero, toleranceAfter: .zero)

resumePlaybackIfNeeded()
}

func goBackwards() async {
guard isLoaded else { return }

/// If needed pause playback to improve seek performance
pausePlaybackIfNeeded()

let time = CMTimeSubtract(
player.currentTime(),
CMTimeMakeWithSeconds(Double(skipInterval), preferredTimescale: 1)
)
await player.seek(to: time, toleranceBefore: .zero, toleranceAfter: .zero)

resumePlaybackIfNeeded()
}

func goToTime(_ timecode: Double) async {
Expand Down Expand Up @@ -383,6 +395,18 @@ import SwiftUI
window.aspectRatio = videoSize
}

private func pausePlaybackIfNeeded() {
guard player.rate != 0 else { return }
wasPausedBeforeSeeking = true
player.rate = 0
}

private func resumePlaybackIfNeeded() {
guard wasPausedBeforeSeeking else { return }
player.rate = player.defaultRate
wasPausedBeforeSeeking = false
}

private func selectTrack(_ option: AVMediaSelectionOption?, in group: AVMediaSelectionGroup) {
guard let item = player.currentItem else { return }
item.select(option, in: group)
Expand Down

0 comments on commit 99caae0

Please sign in to comment.