I'm playing a video for decoration in my UI. I am hiding the AV player controls but it's still possible for the user to control the video. For instance, they can use swipe gestures to fast forward or rewind.
That's particularly surprising to me since the AVPlayerView has an overlay view on top of it.
Does anyone know how prevent all user interaction with this video?
Swipe gestures are generally three fingers, and from what I can tell these have no effect on the playback behavior of AVPlayerView
; scroll gestures (two fingers) are the problem here. To do away with the default scroll-gesture implementation, you just need to override the scrollWheel:
event handler on AVPlayerView
:
import Cocoa
import AVKit
class PPPlayerView: AVPlayerView {
var prohibitScrolling = true
override func scrollWheel(theEvent: NSEvent) {
if prohibitScrolling {
// just swallow the event
} else {
// request default behaviour
super.scrollWheel(theEvent)
}
}
}
Or you can do it with an extension in a new file, let's say in 'AVPlayerViewExtensions.swift' like this:
import Cocoa
import AVKit
extension AVPlayerView {
override open func scrollWheel(with event: NSEvent) {
// Disable scrolling that can cause accidental video playback control (seek)
return
}
override open func keyDown(with event: NSEvent) {
// Disable space key (do not pause video playback)
let spaceBarKeyCode = UInt16(49)
if event.keyCode == spaceBarKeyCode {
return
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With