SeekToTime in AVPlayer stops playing streaming audio when forward

I transmit audio using AVPlayer. It works well. But now I need to make a slider to move the audio back and forth, like a regular music player. I used the seekToTime function, which works fine with a local sound file. But when I transfer the song from the web url, the function stops the sound when I forward the slider with a large range.

My guess is that the song is loading, and if I move the slider forward with a large range, then maybe the system did not load the song data packets during this time, so it stops the player.

Suppose I just clicked a button to transfer a song, but now I move the slider right away for 0 - 100 seconds. In this case, the system does not work and stops the player. Due to the lack of data packets during this time.

Does anyone know how to overcome this problem or is there any other approach. I use SWIFT for development. I have to use any library if it works fast. This is my function:

 func forward () { timeGap = slider.value let preferredTimeScale : Int32 = 1 let targetTime : CMTime = CMTimeMake(timeGap, preferredTimeScale) player.seekToTime(targetTime, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero) } 

Thanks in advance.

+5
source share
1 answer

This answer is pretty much based on this: fooobar.com/questions/270598 / .... Any loan should go there because it is just a quick transfer.

You are right to assume that when searching far ahead to the part that has not yet been buffered, the player stops. The fact is that it still buffers the data, but does not start automatically when it is ready. So rephrase the related answer to a quick one:

To set up your watchers:

 player.currentItem?.addObserver(self, forKeyPath: "playbackBufferEmpty", options: .New, context: nil) player.currentItem?.addObserver(self, forKeyPath: "playbackLikelyToKeepUp", options: .New, context: nil) 

Note that in order to be able to observe the values, the passed self must inherit from NSObject .

And for their processing:

 override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { guard keyPath != nil else { // a safety precaution super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context) return } switch keyPath! { case "playbackBufferEmpty" : if player.currentItem!.playbackBufferEmpty { print("no buffer") // do something here to inform the user that the file is buffering } case "playbackLikelyToKeepUp" : if player.currentItem!.playbackLikelyToKeepUp { self.player.play() // remove the buffering inidcator if you added it } } } 

You can also get information about the available time ranges from the currently playing AVPlayerItem (you can get it through player.currentItem if you did not create it). This allows you to tell the user which parts of the file are ready for use.

As always, you can read a few more documents: AVPlayerItem and AVPlayer

More on key value observation (KVO): here

+8
source

Source: https://habr.com/ru/post/1236614/


All Articles