Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent user from interacting with AVPlayerView?

Tags:

cocoa

avplayer

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?

like image 296
Aaron Avatar asked Mar 10 '16 02:03

Aaron


2 Answers

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) 
        }
    }
}
like image 181
Paul Patterson Avatar answered Sep 18 '22 23:09

Paul Patterson


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
        }
    }

}
like image 27
balazs630 Avatar answered Sep 21 '22 23:09

balazs630