AVPlayer, notification for play/pause state?

后端 未结 5 799
离开以前
离开以前 2021-01-31 15:45

I\'m searching for a way to get notified the exact moment when AVPlayer starts playing. There\'s the \"rate\" property, but currently I am checking it periodically

相关标签:
5条回答
  • 2021-01-31 16:14

    For iOS 10 onwards You can check new property of AVPlayer timeControlStatus.

    if(avPlayerObject.timeControlStatus==AVPlayerTimeControlStatusPaused)
    {
    //Paused mode
    }
    else if(avPlayerObject.timeControlStatus==AVPlayerTimeControlStatusPlaying)
    {
     //Play mode
    }
    
    0 讨论(0)
  • 2021-01-31 16:18

    1)Add below (observer)code to your avPlayer Object

    self.player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
    

    2)And below delegate for that

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
            if (keyPath == "rate") {
                if ((player?.rate) == 1) {
                    print("Plyaing")
    
                } else {
                    print("Pause")
                }
            }
        }
    
    0 讨论(0)
  • 2021-01-31 16:29

    AVPalyer as default observer to track the current duration of the video ,when you pause or resume the video you can get paused time by using one global variable (inside observer update that variable)

    CMTime interval = CMTimeMake(1, 1);
    
    //The capture of self here is coming in with your implicit property access of self.currentduration - you can't refer to self or properties on self from within a block that will be strongly retained by self.
    
    //You can get around this by creating a weak reference to self before accessing timerDisp inside your block
    __weak typeof(self) weakSelf = self;
    
    self.timeObserverToken = [_player addPeriodicTimeObserverForInterval:interval queue:NULL usingBlock: ^(CMTime time)
    {
        _currentDuration = (int)CMTimeGetSeconds (_player.currentTime);
    
        if(!_isPlaying)
        {
            _pausedDuration = _currentDuration;
        }
    }
    
    0 讨论(0)
  • 2021-01-31 16:30

    Why do you say that "rate" is not KVO complaint? It works for me.

    Here is what I did:

    - (void)viewDidLoad
    {
        ...
    
        [self.player addObserver:self forKeyPath:@"rate" options:0 context:nil];
    }
    

    And then:

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"rate"]) {
        if ([self.player rate]) {
            [self changeToPause];  // This changes the button to Pause
        }
        else {
            [self changeToPlay];   // This changes the button to Play
        }
    }
    }
    
    0 讨论(0)
  • 2021-01-31 16:37
        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")
            }
        }
    }
    

    in swift

    0 讨论(0)
提交回复
热议问题