Brian van den heuvel
Brian van den heuvel

Reputation: 396

recognizing a select on Apple TV remote

My app for Apple tv is pulling audio from a URL and playing it. However on pressing the Button again it needs to pause.

therefor i added a addTarget to the UIButton to see if it is playing audio or not.

this is in my ViewDidLoad

player = AVPlayer(url:url)
let playerLayer = AVPlayerLayer(player: player)
let rect = CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 10, height: 50))
playerLayer.frame = rect
self.view.layer.addSublayer(playerLayer)
playButton.addTarget(self, action: "plays:", for: UIControlEvents.primaryActionTriggered )

the function plays

func plays(sender: UIButton!){
    NSLog("Clicked")
    if player?.rate ==  0{
        player!.play()
        print("<PLAYING")
    }else{
        player!.pause()
    }
}

However this gives me the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController plays:]: unrecognized selector sent to instance 0x7fe917802b80'

How can i recognize a 'click' or 'press' on the remote and make the Function plays do its work?

Upvotes: 1

Views: 120

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236370

In Swift 3 you need to an underscore before the first parameter of your method.

func plays(_ sender: UIButton!) { 

And the proper selector syntax:

#selector(plays(_:))

Upvotes: 1

Related Questions