Skip to content
This repository has been archived by the owner on May 30, 2021. It is now read-only.

Commit

Permalink
Fix/live streaming start from beginning (#11)
Browse files Browse the repository at this point in the history
* fix: revise to seek to the target position on initial play

* fix: ensure the initial seeking happens
  • Loading branch information
reedom committed Mar 29, 2021
1 parent 15dc6b1 commit d9e32bf
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions darwin/Classes/WrappedMediaPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ class WrappedMediaPlayer {

var observers: [TimeObserver]
var keyVakueObservation: NSKeyValueObservation?

var bufferingObservation: NSKeyValueObservation?

var isPlaying: Bool
var playbackRate: Float
var volume: Float
var playingRoute: String
var looping: Bool
var url: String?
var onReady: ((AVPlayer) -> Void)?

var initialSeek: CMTime?
var initialSeekSentinel: Int

init(
reference: SwiftAudioplayersPlugin,
playerId: String,
Expand Down Expand Up @@ -48,6 +51,7 @@ class WrappedMediaPlayer {
self.looping = looping
self.url = url
self.onReady = onReady
self.initialSeekSentinel = 0
}

func clearObservers() {
Expand Down Expand Up @@ -191,10 +195,30 @@ class WrappedMediaPlayer {
if reference.isDealloc {
return
}
let millis = fromCMTime(time: time)
// If the initial seeking has not done, use it
// to prevent notifying with a wrong position.
let millis = fromCMTime(time: initialSeek ?? time)
achieveInitialSeek(time)
reference.onCurrentPosition(playerId: playerId, millis: millis)
}


func achieveInitialSeek(_ time: CMTime) {
guard let initialSeek = initialSeek else { return }

if abs(time.seconds - initialSeek.seconds) < 20 {
self.initialSeek = nil
return
}

player?.seek(to: initialSeek)

initialSeekSentinel -= 1
if (initialSeekSentinel <= 0) {
// Given up
self.initialSeek = nil
}
}

func updateDuration() {
guard let duration = player?.currentItem?.asset.duration else {
return
Expand Down Expand Up @@ -225,12 +249,6 @@ class WrappedMediaPlayer {
playerItem.preferredForwardBufferDuration = Double(bufferSeconds)
}

if let time = time {
playerItem.seek(to: time)
} else {
playerItem.seek(to: CMTime.zero)
}

let player: AVPlayer
if let existingPlayer = self.player {
keyVakueObservation?.invalidate()
Expand Down Expand Up @@ -285,6 +303,22 @@ class WrappedMediaPlayer {

keyVakueObservation?.invalidate()
keyVakueObservation = newKeyValueObservation

if let time = time {
initialSeek = time
} else {
initialSeek = CMTime.zero
}
initialSeekSentinel = 3000 / 200 // 3sec / timeObserver's interval

bufferingObservation?.invalidate()
bufferingObservation = playerItem.observe(\AVPlayerItem.isPlaybackLikelyToKeepUp) { [weak self] (playerItem, change) in
if playerItem.isPlaybackLikelyToKeepUp {
if let initialSeek = self?.initialSeek {
self!.player!.seek(to: initialSeek)
}
}
}
} else {
if playbackStatus == .readyToPlay {
if let time = time {
Expand Down

0 comments on commit d9e32bf

Please sign in to comment.