Search code examples
iosswiftstreamavplayeravaudioplayer

Faster seek time in AVPlayer when streaming long music


I'm using Jukebox library to stream remote music. Musics that I stream are 60 min long. When I try to seek them, it takes like 30-40 seconds to play. I changed some codes in the library to make them play immediately for the first time. It worked, but it does not speed up seeking.

 public func seek(toSecond second: Int, shouldPlay: Bool = false) {
    guard let player = player, let item = currentItem else {return}

    player.seek(to: CMTimeMake(Int64(second), 1))
    item.update()
    if shouldPlay {
        if #available(iOS 10.0, *) {
            player.playImmediately(atRate: 1.0)
        } else {
            player.play()
        }
        if state != .playing {
            state = .playing
        }
    }
    delegate?.jukeboxPlaybackProgressDidChange(self)
}

Solution

  • From your comment, I found that function calls everytime you slide the slider. For example It is called 50 times when you move slider from 0 to 50. So you need to create an eventhandler, which will look wen user stops sliding.

    You can get answer from How to detect the end of slider drag answer.