ios AVPlayer,播放/暂停状态通知?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7575494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AVPlayer, notification for play/pause state?
提问by steipete
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.
我正在寻找一种在AVPlayer
开始播放的确切时刻获得通知的方法。有“rate”属性,但目前我正在定期检查它NSTimer
以获取更新。
I tried KVO, but apparently it's not KVO compliant.
我试过 KVO,但显然它不符合 KVO。
I know that there are eventswhen 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.
我也 KVO 订阅了AVPlayerItem's
“状态”,但它会在 HTTP 资产完成缓存时向我显示,没有播放/暂停。我还开始收集所有播放/暂停的调用,之后请求即时 UI 更新,但在AVPlayer
真正开始播放之前需要更多的运行循环。我只想立即更新我的按钮。
采纳答案by The iCoder
For iOS 10onwards You can check new property of AVPlayer timeControlStatus.
对于 i OS 10以后,您可以检查 AVPlayer timeControlStatus 的新属性。
if(avPlayerObject.timeControlStatus==AVPlayerTimeControlStatusPaused)
{
//Paused mode
}
else if(avPlayerObject.timeControlStatus==AVPlayerTimeControlStatusPlaying)
{
//Play mode
}
回答by raixer
Why do you say that "rate" is not KVO complaint? It works for me.
为什么说“rate”不是KVO投诉?这个对我有用。
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
}
}
}
回答by Kumar Swamy
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)
AVPalyer 作为默认观察者来跟踪视频的当前持续时间,当您暂停或恢复视频时,您可以通过使用一个全局变量来获得暂停时间(内部观察者更新该变量)
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;
}
}
回答by Jatin Rathod
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
迅速