如何检测到AVPlayer实际上开始在swift中运行?

时间:2023-01-23 23:06:22

Hello I have set my UISliderminimum value to 0.00. Then I set it's max value in this way.

你好,我把UISliderminimum值设置为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

But I am getting this exception

但我有这个例外

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

I know after prepareForPlay() to actual playing it takes some time to really play the video. So how can I detect when the player really started to play the video? Please help me. Thanks

我知道在准备好播放()之后,真正播放视频需要一些时间。那么,我如何才能检测到玩家何时真正开始播放视频呢?请帮助我。谢谢

5 个解决方案

#1


5  

You can add an observer on the object of your AVPlayer like this

您可以在您的AVPlayer对象上添加一个观察者,如下所示

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

and you can check the status change with your AVPlayer like this

你可以像这样检查状态变化。

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

#2


5  

Here is what I did to actually know when video started (not when it's only ready to start).

以下是我所做的,实际上我知道视频什么时候开始(而不是它什么时候才开始)。

Swift 4

斯威夫特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  

From Apple's 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

从苹果的文档中:你可以观察一个AVPlayerLayer对象的readyForDisplay属性,当这个层有用户可见的内容时,它会被通知。特别是,您可能只在有用户需要查看的内容时,才会将player层插入到层树中,然后再进行转换

#4


1  

Since iOS 10 you can observe timeControlStatus property of AVPlayer. It can be .playing.

因为ios10可以观察到AVPlayer的timeControlStatus属性。它可以.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
            }
        }
    }
}

#5


0  

Declare AVPlayer Global

声明AVPlayer全球

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

#1


5  

You can add an observer on the object of your AVPlayer like this

您可以在您的AVPlayer对象上添加一个观察者,如下所示

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

and you can check the status change with your AVPlayer like this

你可以像这样检查状态变化。

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

#2


5  

Here is what I did to actually know when video started (not when it's only ready to start).

以下是我所做的,实际上我知道视频什么时候开始(而不是它什么时候才开始)。

Swift 4

斯威夫特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  

From Apple's 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

从苹果的文档中:你可以观察一个AVPlayerLayer对象的readyForDisplay属性,当这个层有用户可见的内容时,它会被通知。特别是,您可能只在有用户需要查看的内容时,才会将player层插入到层树中,然后再进行转换

#4


1  

Since iOS 10 you can observe timeControlStatus property of AVPlayer. It can be .playing.

因为ios10可以观察到AVPlayer的timeControlStatus属性。它可以.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
            }
        }
    }
}

#5


0  

Declare AVPlayer Global

声明AVPlayer全球

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