AVplayer currentTime() display wrong duration after seek flac online streaming song

So I try to play online streaming song with format song is flac.

The problem is when I seek song duration the player always end the song at wrong duration like this (video demo on this post):

https://stackoverflow.com/questions/64640693/avplayer-currenttime-display-wrong-duration-after-seek-flac-online-streaming-s

Even though it can be fix by add AVURLAssetPreferPreciseDurationAndTimingKey = true at AVURLAsset options is the any other way to fix this? Because using AVURLAssetPreferPreciseDurationAndTimingKey cause load the song take too long time especially when the connection is poor

Source Code sample:
https://github.com/cendolinside123/sample-musicplayeR

tested on iOS: 13.1.2
my code to handle seek process:
MusicPlayer.swift

Code Block
func seek(timeTo: Double) {
let seekTime = CMTime(seconds: timeTo, preferredTimescale: 1)
isPlaying = .Seek
player.pause()
player.seek(to: seekTime, toleranceBefore: .zero, toleranceAfter: .positiveInfinity, completionHandler: { [weak self] result in
self?.presenter?.checkState_Seek(state: (self?.isPlaying)!)
})
}


ViewController.Swift


Code Block
@IBAction func seeking(_ sender: UISlider) {
let second = Float((duration / 100)) * progressBar.value
MusicPlayer.sharedInstance.seek(timeTo: Double(second))
}


Note: this issue happend only when seek on play streaming online with .flac format if I seek the song, it work normal if it seek on play streaming online with .mp3 and .m4a format



My understanding is that AVURLAssetPreferPreciseDurationAndTimingKey is exactly the correct solution for this sort of problem. The reason it is optional is that it carries performance tradeoffs, as you have discovered.
this is the log of progress time and duration of song :





observer that I use to update the duration and progress time:

Code Block
var timeObserver: Any?
override open func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let item = object as? AVPlayerItem, let keyPath = key, item == playerItem {
switch keyPath {
case "duration":
let duration = Double(item.duration.seconds)
print("Update display Duration in second: \(duration)")
controller?.doUpdateDuration(duration: duration)
self.timeObserver = player.addPeriodicTimeObserver(forInterval: interval, queue: .main, using: { time in
self.playerDelegate?.updateProgresTime(time: time.seconds)
})
default:
break
}
}
}


Update:

this issue happend when play audio file with flac format

hi , Is there any progress on this issue?

AVplayer currentTime() display wrong duration after seek flac online streaming song
 
 
Q