Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayer, notification for play/pause state?

Tags:

ios

avplayer

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 with an NSTimer to get updates.

I tried KVO, but apparently it's not KVO compliant.

I know that there are events when the player ENDED. But i'm talking about pause here.

I also KVO subscribed to AVPlayerItem's "status", but it's showing me when the HTTP asset has finished caching, no play/pause. I also started collecting all calls of play/pause, requesting an instant UI update afterwards, but it takes some more runloops before AVPlayer really starts playing. I'd just love to update my button instantly.

like image 945
steipete Avatar asked Sep 27 '11 20:09

steipete


6 Answers

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
    }
}
}
like image 172
raixer Avatar answered Oct 29 '22 15:10

raixer


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
}
like image 36
The iCoder Avatar answered Oct 29 '22 13:10

The iCoder


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;
    }
}
like image 24
Kumar Swamy Avatar answered Oct 29 '22 14:10

Kumar Swamy


    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

like image 42
Jatin Rathod Avatar answered Oct 29 '22 14:10

Jatin Rathod


If you're targeting iOS 13 and up, you can pull this off elegantly using Combine:

cancellable = myAVPlayerInstance.publisher(for: \.timeControlStatus)
    .sink { [unowned self] status in
       ...
    }

where status is any case of AVPlayer.TimeControlStatus

like image 44
Xtian D. Avatar answered Oct 29 '22 15:10

Xtian D.


Add an observer to your AVPlayer object's rate value:

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

And override the method that will be called when the rate changes:

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "rate", let player = object as? AVPlayer {
        if player.rate == 1 {
            print("Playing")
        } else {
            print("Paused")
        }
    }
}
like image 39
Satya Avatar answered Oct 29 '22 15:10

Satya