Posts

Post not yet marked as solved
3 Replies
3.2k Views
I have a problem where a gestureRecognizer for the play/pause button stops working after playing a video with a AVPlayerViewController on tvOS 10.This problem only appears on the physical device and not the simulator.The problem only occurs if I play an audio file before or after presenting the video in a AVPlayerViewController. I have distilled the issue to a app with a single viewController and storyboard. I created an UIButton in IB and set it to fire the buttonPressed action.import Foundation import UIKit import AVKit import AVFoundation class ViewController: UIViewController { var playPause = false var audioPlayer:AVAudioPlayer? override func viewDidLoad() { super.viewDidLoad() playAudioFile() enablePlayPauseHandling() } func playAudioFile() { if let asset = NSDataAsset(name:"mp3") { do { try audioPlayer = AVAudioPlayer(data: asset.data) } catch { print("exception") } } audioPlayer?.prepareToPlay() audioPlayer?.play() } func enablePlayPauseHandling() { let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapped(_:))) tapRecognizer.allowedPressTypes = [NSNumber(value: UIPressType.playPause.rawValue as Int)]; self.view.addGestureRecognizer(tapRecognizer) } func tapped(_ sender:UITapGestureRecognizer) { playPause = !playPause setBackgroundColor() } func setBackgroundColor() { if playPause { self.view.backgroundColor = UIColor.black } else { self.view.backgroundColor = UIColor.white } } @IBAction func buttonPressed(_ sender: AnyObject) { self.audioPlayer?.pause() let playerItem = AVPlayerItem(url: url) let playerController = AVPlayerViewController() playerController.player = AVPlayer(playerItem: playerItem) self.present(playerController, animated: true) { playerController.player?.play() } } }Whenever the play/pause button is pressed on the remote the view changes back ground color as visual indicator that it worked.Running this in the simulator works as expected. I can press the button on the screen to play the video and press the menu button on the remote to stop the video. After the video has played pressing the play/pause button still works as expected.On the the physical Apple TV though, the play/pause button stops working after the video is played. If I were to comment out line 12 where I call playAudioFile() in the viewDidLoad function, the play/pause button keeps on working after the video played.In summary there appears to be an issue with the play/Pause button and the combination of playing both an audio file and video on the physical device.Does any one have any ideas or suggestions for work arounds.Thanks
Posted Last updated
.