VideoPlayer/AVQueuePlayer does not seek to time

343 Views Asked by At

The AVQueuePlayer does not seek to the given time. Video always starts playing at the beginning. Using the default controls, I can seek manually to a desired point. I am using a m3u8 HLS stream.

import SwiftUI
import AVKit

struct ChannelPlayerView: View {
  @ObservedObject var channelViewModel: ChannelViewModel
  @State private var player: AVQueuePlayer?
  @State private var videoLooper: AVPlayerLooper?


// VideoPlayer example from https://www.raywenderlich.com/19074315-swiftui-on-tvos
  var body: some View {
    VideoPlayer(player: player)
      .onAppear {
        if player == nil {
          let videoURL = self.channelViewModel.currentVideo().masterUrl // example.com/video/master.m3u8
          
          // Will be used after seeking works
          //let currentTimesamp = Date() - video.startingTime
          //let cmTime = CMTime(seconds: currentTimesamp, preferredTimescale: 1000000)

          let templateItem = AVPlayerItem(url: URL(string: videoURL)!)
          player = AVQueuePlayer(playerItem: templateItem)
          videoLooper = AVPlayerLooper(player: player!, templateItem: templateItem)¡
        }

        if player?.isPlaying == false {
          let playerTimescale = self.player!.currentItem?.asset.duration.timescale ?? 1
          let time =  CMTime(seconds: 77.000000, preferredTimescale: playerTimescale)
          player?.seek(to: time)
          player?.play() // Tried with play() and without
        }
      }
      .edgesIgnoringSafeArea(.all)
  }
}

Here is the m3u8 File that is given to the player.

#EXTM3U
# Created with Bento4 mp4-hls.py version 1.2.0r637

#EXT-X-VERSION:4

# Media Playlists
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=3072204,BANDWIDTH=4633702,CODECS="avc1.4D402A,mp4a.40.2",RESOLUTION=1920x1080
media-1/stream.m3u8
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=1576056,BANDWIDTH=2443812,CODECS="avc1.4D402A,mp4a.40.2",RESOLUTION=1280x720
media-2/stream.m3u8
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=896410,BANDWIDTH=1351872,CODECS="avc1.4D402A,mp4a.40.2",RESOLUTION=852x480
media-3/stream.m3u8
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=637434,BANDWIDTH=940939,CODECS="avc1.4D402A,mp4a.40.2",RESOLUTION=640x360
media-4/stream.m3u8
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=418851,BANDWIDTH=579212,CODECS="avc1.4D402A,mp4a.40.2",RESOLUTION=426x240
media-5/stream.m3u8

# I-Frame Playlists
#EXT-X-I-FRAME-STREAM-INF:AVERAGE-BANDWIDTH=226870,BANDWIDTH=4053137,CODECS="avc1.4D402A",RESOLUTION=1920x1080,URI="media-1/iframes.m3u8"
#EXT-X-I-FRAME-STREAM-INF:AVERAGE-BANDWIDTH=132899,BANDWIDTH=2336684,CODECS="avc1.4D402A",RESOLUTION=1280x720,URI="media-2/iframes.m3u8"
#EXT-X-I-FRAME-STREAM-INF:AVERAGE-BANDWIDTH=83388,BANDWIDTH=1363065,CODECS="avc1.4D402A",RESOLUTION=852x480,URI="media-3/iframes.m3u8"
#EXT-X-I-FRAME-STREAM-INF:AVERAGE-BANDWIDTH=57207,BANDWIDTH=908710,CODECS="avc1.4D402A",RESOLUTION=640x360,URI="media-4/iframes.m3u8"
#EXT-X-I-FRAME-STREAM-INF:AVERAGE-BANDWIDTH=31981,BANDWIDTH=490415,CODECS="avc1.4D402A",RESOLUTION=426x240,URI="media-5/iframes.m3u8"
1

There are 1 best solutions below

0
jussi On BEST ANSWER

Found out the problem.

I need to seek on the currentItem, not on the AVQueuePlayer. Did not found a clue on why in the documentation.

          player?.currentItem!.seek(to: cmTime, toleranceBefore: .zero, toleranceAfter: .zero, completionHandler: nil)