如何对AVPlayer的播放和暂停操作执行某些操作? [英] How to perform some action on Play and Pause actions of AVPlayer?
问题描述
我使用 AVPlayer
在我的swift应用程序上播放视频。我想在视频中间放置一个播放按钮(自定义按钮)。我知道如何在点击按钮时播放和暂停视频。
I have used AVPlayer
to play video on my swift application. I want to place a Play button (custom button) at the middle of video. I know how to play and pause video on tapping the button.
从默认按钮播放/暂停视频时,我找不到如何显示/隐藏自定义按钮?播放/暂停视频时是否有任何听众或触发器?
I couldn't find how to show/hide the custom button when the video is played/paused from default buttons? Are there any listener or something which triggers when the video is played/paused?
我还想隐藏默认控件中的音频和字幕图标。
I also want to hide the "Audio & Subtitles" icon from default control.
推荐答案
以下是您可以听取播放器状态更改和隐藏/显示按钮的方法:(假设您的播放器名为播放器
,按钮为 playButton
)
Here is how you can listen to player state changes and hide/show buttons: (assume your player is named player
, and button is 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,则意味着视频不是播放。
If rate of player is equal to 0 it means that video is not playing.
这篇关于如何对AVPlayer的播放和暂停操作执行某些操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!