2

I am using an AVPlayer object to play a live stream of an MP3, just using an HTTP link.

As with normal live video playing in iOS, there is a button that you can press to skip to live.

Is it possible to do this with AVPlayer?

E.g I am listening live, pause, then after however long play again. It continues from where I left it. But what if I want to skip to live?

Josh Kahane
  • 16,765
  • 45
  • 140
  • 253

1 Answers1

0

I had the same need and made this extension to AVPlayer:

extension AVPlayer {
    func seekToLive() {
        if let items = currentItem?.seekableTimeRanges, !items.isEmpty {
            let range = items[items.count - 1]
            let timeRange = range.timeRangeValue
            let startSeconds = CMTimeGetSeconds(timeRange.start)
            let durationSeconds = CMTimeGetSeconds(timeRange.duration)

            seek(to: CMTimeMakeWithSeconds(startSeconds + durationSeconds, 1))
        }
    }
}
jalmaas
  • 908
  • 8
  • 17