Trev347
Trev347

Reputation: 324

While using AVPlayerViewController, video plays in the background when user hits home button, but when returning to app the video closes by itself

I have a AVPlayerViewController, and when the user clicks on the home button and runs the app in the background it works fine and continues to play.

My problem is when I return to the app by clicking on the fullscreen button in the top right corner of my video, the video closes out by itself and the user has to click on the video again and find the spot they were at. Obviously this is a bad user experience and I want to fix this. Any ideas on how I can fix this?

Here is an example video of what is happening. example

Here is our video player class.

class VideoPlayer: AVPlayerViewController {
    
    init(url: URL) {
        super.init(nibName: nil, bundle: nil)
        player = AVPlayer(url: url)
        let audio = AVAudioSession()
        do {
            try audio.setCategory(.playback, mode: .moviePlayback)
        } catch {
            
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

Here is how we present it.

        imageViewButton.addAction(UIAction() { _ in
            self.vc?.present(VideoPlayer(url: post.movie!), animated: true)
        }, for: .touchUpInside)

Upvotes: 2

Views: 359

Answers (1)

Joonaynay
Joonaynay

Reputation: 49

In your video player class you need to add

delegate = self

and then

func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_ playerViewController: AVPlayerViewController) -> Bool {
    return false
}

Upvotes: 1

Related Questions