gpt4 book ai didi

swift - AVPlayerItemDidPlayToEndTime 永远不会到达 Swift 3 上的选择器

转载 作者:搜寻专家 更新时间:2023-10-31 08:17:56 26 4
gpt4 key购买 nike

我正在使我的应用程序适应 Swift 3,但我遇到了这个问题。这曾经适用于 Swift 2.2,但现在已损坏。 moviePlayBackFinished 永远不会被调用。我尝试以多种方式添加观察者,但都没有成功。

    NotificationCenter.default.addObserver(self, selector: #selector(self.moviePlayBackFinished(_:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem);

NotificationCenter.default.addObserver(self, selector: #selector(moviePlayBackFinished(_:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem);

NotificationCenter.default.addObserver(self, selector: #selector(self.moviePlayBackFinished(sender:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem);

// (...)

@objc func moviePlayBackFinished(sender : AnyObject) {
print("playbackFinished");
let zeroCM : CMTime = CMTime(seconds: 0, preferredTimescale: 1000000000);
playerLayer.player?.seek(to: zeroCM);
}

@objc func moviePlayBackFinished(_ notification: Notification) {
print("playbackFinished");
let zeroCM : CMTime = CMTime(seconds: 0, preferredTimescale: 1000000000);
playerLayer.player?.seek(to: zeroCM);
}

任何想法都会得到应用。

谢谢

最佳答案

这个问题有多种可能的原因。

NotificationCenter
.default
.addObserver(self,
selector: #selector(self.moviePlayBackFinished(sender:)),
name: .AVPlayerItemDidPlayToEndTime,
object: player.currentItem)

首先,您必须将对象设置为零。 AVPlayerItemDidPlayToEndTime 通知自动将对象设置为到达终点的 AVPlayerItem。如果您手动更改它,您将不会收到任何 AVPlayerItemDidPlayToEndTime 的通知。

其次,根据 AVPlayerItemDidPlayToEndTime 文档:

Important

This notification may be posted on a different thread than the one on which the observer was registered.

因此,为了确保您应该检查通知是否在您想要的线程上。

UIViewControllerAVPlayer 子类添加观察者并不重要。

关于swift - AVPlayerItemDidPlayToEndTime 永远不会到达 Swift 3 上的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39669002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com