rai*_*xer 47
为什么你说"费率"不是KVO投诉?这个对我有用.
这是我做的:
- (void)viewDidLoad
{
...
[self.player addObserver:self forKeyPath:@"rate" options:0 context:nil];
}
Run Code Online (Sandbox Code Playgroud)
然后:
- (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
}
}
}
Run Code Online (Sandbox Code Playgroud)
The*_*der 17
对于i OS 10以上版本,您可以检查AVPlayer timeControlStatus的新属性.
if(avPlayerObject.timeControlStatus==AVPlayerTimeControlStatusPaused)
{
//Paused mode
}
else if(avPlayerObject.timeControlStatus==AVPlayerTimeControlStatusPlaying)
{
//Play mode
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您的目标是 iOS 13 及更高版本,您可以使用以下方法优雅地完成此任务Combine
:
cancellable = myAVPlayerInstance.publisher(for: \.timeControlStatus)
.sink { [unowned self] status in
...
}
Run Code Online (Sandbox Code Playgroud)
哪里status
有任何case
一个AVPlayer.TimeControlStatus