How to detect AVPlayer actually started to play in swift

后端 未结 7 1462
情话喂你
情话喂你 2020-12-16 10:59

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

self.viewPlayer.layer.addSublayer(playerLayer)
    le         


        
相关标签:
7条回答
  • 2020-12-16 11:23

    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

    0 讨论(0)
  • 2020-12-16 11:25

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

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-16 11:26

    Declare 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 讨论(0)
  • 2020-12-16 11:28

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

    Where videoPlayer is of type AVPlayer, obviously.

    0 讨论(0)
  • 2020-12-16 11:29

    SUPER EASY SOLUTION SWIFT 4-5:

    Just check the timeControlStatus!: (also works with PlayerQueue)

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

    the raw value will give you its current state :)

    0 讨论(0)
  • 2020-12-16 11:32

    Since iOS 10 you can observe timeControlStatus property of AVPlayer. It can 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
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题