如何检测AVPlayer实际上开始在swift中播放

时间:2016-11-24 08:55:11

标签: ios swift3 avplayer uislider

您好我已将UISlider最小值设置为0.00。然后我以这种方式设置它的最大值。

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

但是我得到了这个例外

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

我知道prepareForPlay()实际播放后需要一些时间来真正播放视频。那么如何才能检测到播放器何时真正开始播放视频? 请帮我。 感谢

8 个答案:

答案 0 :(得分:15)

从iOS 10开始,您可以观察到AVPlayer的timeControlStatus属性。它可以是.playing

检查代码:

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
            }
        }
    }
}

答案 1 :(得分:10)

您可以像这样

在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)
    }
}

答案 2 :(得分:7)

以下是我在视频开始时实际知道的事情(而不是当它只准备开始时)。

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")
        }
    }
}

答案 3 :(得分:1)

来自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

答案 4 :(得分:0)

声明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()
    }

答案 5 :(得分:0)

另一种更简单的方法是:

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

显然,其中videoPlayer是AVPlayer类型。

答案 6 :(得分:0)

layerObserver = newLayer.observe(\AVPlayerLayer.isReadyForDisplay, options: .new) { [weak self] _, change in
    if change.newValue == true {
        self?.viewModel?.videoLayerReadyToDisplay()
    }
}

应用收到此事件后,意味着您可以隐藏缩略图(例如)并开始显示视频。

答案 7 :(得分:0)

超级简便解决方案SWIFT 4-5:

只需检查timeControlStatus!:(也适用于PlayerQueue)

    if avPlayer?.timeControlStatus.rawValue == 2 {
       
    //video is playing (playing)

    } else if avPlayer?.timeControlStatus.rawValue == 0 { 
    
    //video is not playing (paused)
    
    }

原始值将为您提供其当前状态:)

相关问题