问题
I have several AVPlayerViewControllers
set as the ViewControllers
of a UITabBarController
. What I want is having the video of the currently visible AVPlayerViewController
automatically pause playing when the user switches to another tab (which is another AVPlayerViewController
)
I tried this approach:
override func viewWillDisappear(animated: Bool) {
player?.pause()
super.viewWillDisappear(animated)
}
but the video just keeps running in the background. (audio is still running at least) Debugger says player property is not nil in viewWillDisappear. I already tried implicitly and forced unwrapping, to no avail.
回答1:
I was able to identify the problem. It seems to be that you can't pause in viewWillDisappear
, neither with pause()
nor with player?.rate = 0.0
. So in order to prevent your video from playing in the background, you have to set AVPlayerViewController
's player
property to nil in viewWillDisappear
.
Which sadly means that you have to write some code for preserving your playback state.
Seems a bit like a bug to me, hope this will get fixed later.
来源:https://stackoverflow.com/questions/33840994/avplayerviewcontroller-tvos-doesnt-pause-on-viewwilldisappear