VideoPlayer/AVQueuePlayer不求时间

VideoPlayer/AVQueuePlayer does not seek to time

AVQueuePlayer 没有定位到给定的时间。视频总是从头开始播放。 使用默认控件,我可以手动搜索到所需的点。 我正在使用 m3u8 HLS 流。

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)
  }
}

这是给播放器的m3u8文件。

#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"

找到问题了。

我需要在 currentItem 上寻找,而不是在 AVQueuePlayer 上寻找。未在文档中找到有关原因的线索。

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