play Pause gestureRecognizer stops working after playing video with AVPlayerViewController.

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

Replies

I have a similar problem. Using a UITabBarController, an embedded AVPlayer in one view controller, the other one shows a AVPlayerViewController.

After playing the video in the AVPlayerViewController the gesturerecognizer in the first tab wont get recognized anymore...

Hi Shobba and louisvanwijk,


I'm also having the same issue. I'm using TVMLKit for the main bulk of my app, but also use AVAudioPlayer to play music assets inline. I'm also using an AVPlayerViewController in an embedded controller to play video (I can't use the JS player for various reasons. Playing the video is fine, but when returning to the TVML view, I lose playback controls once I attempt to start music again.


Did either of you two have any luck resolving your issues?

Hi. It's an old topic but looks like we're still experiencing this issue on tvOS 14. Any known workarounds? Seems to work on emulator but not on real device.