AVPlayer边界时间观察器偶尔无法触发 [英] AVPlayer boundary time observer fails to fire occasionally

查看:120
本文介绍了AVPlayer边界时间观察器偶尔无法触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVPlayer的-addBoundaryTimeOserverForTimes:queue:usingBlock:在视频中的特定时间执行一些代码(在这种情况下,我希望在视频达到其时长时取消隐藏按钮.代码如下) :

I'm using AVPlayer's -addBoundaryTimeOserverForTimes:queue:usingBlock: to execute some code at a specific time in my video (in this case, I want a un-hide a button when my video reaches its duration. Code is as follows:

- (void)viewWillAppear:(BOOL)animated
{
    ...

    _player = [AVPlayer playerWithURL:videoURL];

    AVPlayerLayer *newPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    [newPlayerLayer setFrame:_videoView.bounds];
    [_videoView.layer addSublayer:newPlayerLayer];

    _observer = [_player addBoundaryTimeObserverForTimes:@[[NSValue valueWithCMTime:_player.currentItem.duration]] queue:NULL usingBlock:^{
        [someButton setHidden:NO];
    }];

    ...
}

无论出于何种原因,有时代码块都会触发,并且按钮变为可见,有时却不可见.尚未能够找到这种行为的模式.它经常(几乎总是)在模拟器中发生,并且偶尔在设备上发生.有没有人遇到这个问题?有任何想法可能会发生什么事吗?

For whatever reason, sometimes the block of code fires and the button becomes visible, and sometimes it doesn't. Haven't been able to find a pattern in this behavior. It happens very often (almost always) in the simulator, and occasionally when on a device. Has anyone encountered this problem? Any ideas what might be going on?

修改

此外,如果我在块上放置一个断点,它总是会触发.

Also, if I put a breakpoint on the block, it ALWAYS fires.

推荐答案

对于那些希望观察玩家何时结束的人:

我每年大约浏览一次这个问题,因为我总是忘了最适合我的解决方案.这次我在MacOS 上遇到了这个问题.我看到的是相同的行为,观察者块有时不会被调用.当我从正在调试为Xcode的应用程序切换回时,该块突然触发.这可能与OP中描述的在块中设置断点有关.

I browse this question about once a year because I always forget about the fix that works for me. This time around I had this issue on macOS. I am seeing the same behavior, the observer block sometimes does not get called. When I switch back from the app that is being debugged to Xcode the block suddenly fires. This might be related to having a breakpoint set in the block as described by the OP.

但是,这是解决方法:只需按照此答案中的说明切换到AVPlayerItemDidPlayToEndTimeNotification.但是请注意,顾名思义,通知的对象是播放器的当前项目,而不是播放器本身!

Here's the fix however: Simply switch to AVPlayerItemDidPlayToEndTimeNotification as described in this answer. Note however, as the name implies the notification's object is the player's current item not the player itself!

因为此通知是在商品的结束时间触发的,所以如果您需要的时间比商品的实际结束时间(即持续时间)长,则只需设置商品的forwardPlaybackEndTime即可,而不是观察某些边界时间".

Because this notification triggers at the end time of an item, instead of observing some "boundary time" simply set the item's forwardPlaybackEndTime if you need another time than the item's actual end time, i.e. duration.

这篇关于AVPlayer边界时间观察器偶尔无法触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆