How to detect AVPlayer actually started playing in fast

Hello, I set the minimum value UISliderto 0.00. Then I set the maximum value this way.

self.viewPlayer.layer.addSublayer(playerLayer)
    let duration : CMTime = avPlayer.avPlayer.currentItem!.asset.duration
    let seconds : Float64 = CMTimeGetSeconds(duration)

    sliderBar.maximumValue=Float(seconds)
    sliderBar!.isContinuous = false
    sliderBar!.tintColor = UIColor.green

But I get this exception

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempting to set a slider minimumValue (0.000000) to be larger than the maximumValue (nan)'
enter code here

I know that after a prepareForPlay()real game it takes some time to really play the video. So, how can I determine when a player really started playing a video? Please help me. Thanks

+19
source share
7 answers

With firmware 10, you can observe the timeControlStatusAVPlayer property. It could be .playing.

Check the code:

private func setupAVPlayer() {
    avPlayer.addObserver(self, forKeyPath: "status", options: [.old, .new], context: nil)
    if #available(iOS 10.0, *) {
        avPlayer.addObserver(self, forKeyPath: "timeControlStatus", options: [.old, .new], context: nil)
    } else {
        avPlayer.addObserver(self, forKeyPath: "rate", options: [.old, .new], context: nil)
    }
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if object as AnyObject? === avPlayer {
        if keyPath == "status" {
            if avPlayer.status == .readyToPlay {
                avPlayer.play()
            }
        } else if keyPath == "timeControlStatus" {
            if #available(iOS 10.0, *) {
                if avPlayer.timeControlStatus == .playing {
                    videoCell?.muteButton.isHidden = false
                } else {
                    videoCell?.muteButton.isHidden = true
                }
            }
        } else if keyPath == "rate" {
            if avPlayer.rate > 0 {
                videoCell?.muteButton.isHidden = false
            } else {
                videoCell?.muteButton.isHidden = true
            }
        }
    }
}
+17
source

AVPlayer,

player.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.new, context: nil)

AVPlayer,

 func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutableRawPointer) {
    if keyPath == "status" {
        print(player.status)
    }
}
+10

, ( ).

Swift 4

player = AVPlayer(url: URL(fileURLWithPath: path))
player.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions.new, context: nil)

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "rate" {
        if player.rate > 0 {
            print("video started")
        }
    }
}
+7

Apple docs: You can observe an AVPlayerLayer object’s readyForDisplay property to be notified when the layer has user-visible content. In particular, you might insert the player layer into the layer tree only when there is something for the user to look at and then perform a transition from

+1

AVPlayer Global

var streamPlayer = AVPlayer()

func playStreamAudio( for audioUrl:String)
    {
        guard streamPlayer.timeControlStatus != .playing else {
            streamPlayer.pause()
            return
        }
        let url = audioUrl //"http://192.168.71.11:7891/rec.wav"
        let playerItem = AVPlayerItem(url: URL(string:url)!)
        streamPlayer = AVPlayer(playerItem:playerItem)
        streamPlayer.rate = 1.0;
        streamPlayer.volume = 1.0
        streamPlayer.play()
    }
0
layerObserver = newLayer.observe(\AVPlayerLayer.isReadyForDisplay, options: .new) { [weak self] _, change in
    if change.newValue == true {
        self?.viewModel?.videoLayerReadyToDisplay()
    }
}

, , () .

0
source

Another, simpler approach is something like:

if videoPlayer.rate != 0 && videoPlayer.error == nil {
            print("video player is playing.................")
        } else {
            print("video player is NOT playing.")
        } 

Obviously, where videoPlayer is of type AVPlayer.

-1
source

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


All Articles