从头开始播放HLS直播

时间:2017-04-18 12:18:53

标签: ios swift avplayer hls

我有一个事件类型m3u8播放列表,我正在使用AVPlayer播放。这一切都很好,除了寻找过去的时间。暂停时,我可以随时寻找我想要的,并且我在播放器中看到正确的更新帧,但是无论何时我点击播放它都会回到最后~10秒的流并从那里播放。

这是我正在使用的播放列表,我使用JWPlayer HLS测试进行测试,效果很好。

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-TARGETDURATION:4
#EXT-X-PLAYLIST-TYPE:EVENT
#EXT-X-ALLOW-CACHE:YES
#EXT-X-DISCONTINUITY
#EXTINF:2.000,
0.ts
#EXTINF:2.000,
1.ts
#EXTINF:2.000,
2.ts
#EXTINF:2.000,
3.ts
#EXTINF:2.000,
...

我正在使用的代码倒带:

func seek(to time: TimeInterval, play: Bool = false) {
    guard let currentItem = avPlayer.currentItem else { return }
    if avPlayer.status != .readyToPlay || currentItem.status != .readyToPlay { return }

    let time = CMTimeMakeWithSeconds(time, currentItem.currentTime().timescale)

    if time != chaseTime {
        avPlayer.pause()

        chaseTime = time
        if !seekInProgress {
            actuallySeekToTime(play: play)
        }
    }
}

private func actuallySeekToTime(play: Bool) {
    seekInProgress = true
    let seekTimeInProgress = chaseTime!
    avPlayer.seek(to: seekTimeInProgress, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero) { [weak self] finished in
        guard let strongSelf = self else { return }

        if seekTimeInProgress == strongSelf.chaseTime {
            strongSelf.seekInProgress = false
            if play {
                strongSelf.avPlayer.play()
            }
        } else {
            strongSelf.actuallySeekToTime(play: play)
        }
    }
}

0 个答案:

没有答案