简体   繁体   中英

AVPlayer seeks to wrong position in a HLS Audio Stream

I'm trying to play a HLS Audio Streaming using AVPlayer. Everything works fine, but when I seek the player, the currentTime of the player goes to a wrong position.

If I want to seek to 38s, after the seek function executes, my currentTime printed inside the seek completion handler gives me 10s, eg. Most of the time, the player starts from a different position after seeking.

Here is the code I'm using:

let cmTime = CMTime(seconds: validTime, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
self.player.seek(to: cmTime, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero, completionHandler: {  (finished) in
     self.player.currentTime().seconds
})

I saw that AVFoundation does not work properly with VBR files, but all my files are CBR. They all have a constant bit rate of 128kb/s. I segmented my .mp3 file to some .ts files to play as streaming too.

I would like to know if anybody has a solution for this issue or any workaround that could help me with this problem.

Thank you, guys!

I know this was asked long ago, but can you show me your m3u8 file?, HLS use fragments for every file, so let's say every fragment is 5s long, what is going to happen is when you seek, the play will try to stick to the begining of one of the fragments, seeking to 28s will seek to either 25s or 30s.

Still, there's a solution, you can initialize your AVURLAsset using an option like this:

let options = [AVURLAssetPreferPreciseDurationAndTimingKey : true] 
let asset = AVURLAsset.init(url: urlStr, options:options)
...
// Init playerItem and a player

But, loading times should be longer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM