AVPlayer, notification for play/pause state?

后端 未结 5 798
离开以前
离开以前 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: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
        }
    }
    }
    

提交回复
热议问题