Manikandan
Manikandan

Reputation: 321

AVPlayercontroller - Adding Overlay Subviews

Depends upon the PlaybackControl Visibity How to show and hide the Custom Subview added in contentOverlayView?

I want to do like Youtube TVOS app did.

I tried with UIPressesEvent but it is not giving me the exact touch events. It is giving me these:

  override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
    for item in presses {
        switch item.type {
        case .Menu:
            self.customViews.alpha = 0
        case .PlayPause:
            self.player?.pause()
            self.customViews.alpha = 0
        default:
            self.setVisibilityToPreviewView()
        }
      }
    }

 func setVisibilityToPreviewView () { //This wont work in all cases.
      if self.previewView.alpha == 1 {
         self.previewView.alpha = 0
      } else {
         self.previewView.alpha = 1
      }
    }

But with this Touch events i can only show and hide the subviews. It should be hidden when the playbackcontrol is Hidden.

If I get the PlayBackControl Visibility values I don't need to worry about hiding these subviews.

Apple is Using AVNowPlayingPlaybackControlsViewController. It is not open for developers.

So I need to find some other better way to do this.

Please guide me how to do it.

Upvotes: 4

Views: 1865

Answers (1)

Adnan Aftab
Adnan Aftab

Reputation: 14477

You can register a tapGesture recognizer and then set its allowPressTypes property to UIPressType.Select, something like this

let tapRecognizer = UITapGestureRecognizer(target: self, action: "onSelect:")
tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)];
self.view.addGestureRecognizer(tapRecognizer)

And inside your action button show or hide custom overlays.

Example: Add this code inside a view controller and on tap (selecting at empty area on front of remote, touch area) you will see a message on console.

override func viewDidLoad() {
        super.viewDidLoad()
        let tapGesture = UITapGestureRecognizer(target: self, action: "onSelect")
        tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
        self.view.addGestureRecognizer(tapGesture);
    }
    func onSelect(){
        print("this is select gesture handler method");
    }

Update: Below is the code which will create AVPlayerController and will register tapGestureRecognizer to playervc.view.

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        playContent()
    }
    func playContent(){
        let urlString = "<contentURL>"
        guard let url = NSURL(string: urlString) else{
            return
        }
        let avPlayer = AVPlayer(URL: url)
        let playerVC = AVPlayerViewController()
        playerVC.player = avPlayer
        self.playerObj = playerVC

        let tapGesture = UITapGestureRecognizer(target: self, action: "onSelect")
        tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
        self.view.addGestureRecognizer(tapGesture);

        playerVC.view.addGestureRecognizer(tapGesture)
        self.presentViewController(playerVC, animated: true, completion: nil);

    }

Give a try to this, I think it should work fine.

Upvotes: 4

Related Questions