My app plays video throughout several vcs using AVFoundation. For example FirstController plays a video then a user can push on SecondController which also plays a video then they can push on the ThirdController which also plays a video... The same thing would apply if their switching tabs. There's a video screen on TabOne, TabTwo, and TabThree.
Instead of setting up all the playLayer code associated with AVFoundation in each class I created one class that contains a AVPlayerViewController()
and add that class to each vc using addChildViewController()
.
The problem is since I have one class that manages AVFoundation the Notification.Name.AVPlayerItemDidPlayToEndTime
that gets notified when the player finishes playing can't distinguish one video on one vc from another video in a different vc. For example after a video finishes playing I show a replayButton. If the video in the first tab is playing, when I switch to TabTwo I pause that video, after the video on TabTwo finishes and the replayButton appears, if I switch back to TabOne, the replayButton will also appear on TabOne's screen (it should still show the pause button).
The problem is even though I have different instances of the AVFoundationManager
, all the instances access the one showReplayButton() function
that gets triggered when the notification fires.
How can I get around this?
I know I can check on the parent
of the AVFoundationManager to find out which parent is managing it and use that inside the showReplayButton()
function but I don't know which check to run on it.
AVFoundationManager:
class AVFoundationManager: UIViewController {
....
override func viewDidLoad() {
super.viewDidLoad()
configureAVPlayerController()
}
func configureAVPlayerController() {
let avPlayerVC = AVPlayerViewController()
avPlayerVC.player = player
avPlayerVC.view.frame = view.bounds
avPlayerVC.showsPlaybackControls = false
avPlayerVC.videoGravity = AVLayerVideoGravity.resizeAspectFill.rawValue
addChildViewController(avPlayerVC)
view.addSubview(avPlayerVC.view)
avPlayerVC.didMove(toParentViewController: self)
player?.replaceCurrentItem(with: playerItem!)
player?.play()
NotificationCenter.default.addObserver(self, selector: #selector(showReplayButton), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
playerItem?.addObserver(self, forKeyPath: #keyPath(AVPlayerItem.status),
options: [.old, .new],
context: &itemContext)
}
@obj func showReplayButton(){
// if self.parent ... run a bool on the parent and enclose these two in the paranthesis?
pausePlayButton.isHidden = true
replayButton.isHidden = false
}
}
TabOneClass:
let avFoundationManager = AVFoundationManager()
addChildViewController(avFoundationManager)
avFoundationManager.didMove(toParentViewController: self)
TabTwoClass:
let avFoundationManager = AVFoundationManager()
addChildViewController(avFoundationManager)
avFoundationManager.didMove(toParentViewController: self)
FirstController (root) in TabThree:
let avFoundationManager = AVFoundationManager()
addChildViewController(avFoundationManager)
avFoundationManager.didMove(toParentViewController: self)
SecondController (child) in TabThree:
let avFoundationManager = AVFoundationManager()
addChildViewController(avFoundationManager)
avFoundationManager.didMove(toParentViewController: self)