[英]How to perform some action on Play and Pause actions of AVPlayer?
我使用AVPlayer
在我的 swift 應用程序上播放視頻。 我想在視頻中間放置一個播放按鈕(自定義按鈕)。 我知道如何在點擊按鈕時播放和暫停視頻。
當視頻從默認按鈕播放/暫停時,我找不到如何顯示/隱藏自定義按鈕? 播放/暫停視頻時是否有任何偵聽器或觸發的東西?
我還想從默認控件中隱藏“音頻和字幕”圖標。
以下是如何監聽播放器狀態更改和隱藏/顯示按鈕:(假設您的播放器名為player
,按鈕為playButton
)
player.addObserver(self, forKeyPath: "rate", options: .New, context: nil) // somewhere after player init
...
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "rate" {
playButton.enabled = player.rate == 1
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
如果播放器的速率等於 0,則表示視頻未播放。
玩家初始化然后,
self.player?.addObserver(self, forKeyPath: "rate", options: .new, context: nil)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if self.player?.rate == 0 {
print("Pause")
}else {
print("Play")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.