Skip to content

Instantly share code, notes, and snippets.

@ChrisMash
Created March 10, 2020 19:09
Show Gist options
  • Save ChrisMash/57141446fc18771e541571f89a5cc1c5 to your computer and use it in GitHub Desktop.
Save ChrisMash/57141446fc18771e541571f89a5cc1c5 to your computer and use it in GitHub Desktop.
A SwiftUI View to display an AVPlayer's currentTime from PlayerTimeObserver's publisher
import SwiftUI
import AVFoundation
struct PlayerTimeView: View {
let timeObserver: PlayerTimeObserver
@State private var currentTime: TimeInterval = 0
var body: some View {
Text("\(Utility.formatSecondsToHMS(currentTime))")
.onReceive(timeObserver.publisher) { time in
self.currentTime = time
}
}
}
// Create elsewhere like so:
PlayerTimeView(timeObserver: PlayerTimeObserver(player: player))
@Aiyush-G
Copy link

Hiya, how would I use this with a video. I'm quite new so sorry for the potentially straight forward question. If I wanted to stream a video from a URL ie. This one. I would also like to do something like: if current time is X (ie. 10 seconds) then pause video etc... Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment