简体   繁体   中英

AVPlayerViewController memory leak/retain cycle?

I keep getting a problem with my app where AVPlayerViewController won't get purged from memory after being dismissed, it just builds up more after each presentation.

This is on iOS 12, try the following in an empty project, I do it in viewDidAppear for instance to automatically make it pop up a few seconds after it's dismissed. You'll notice after a bunch of dismissals if you go to the "Debug Memory Graph" tool at the bottom of Xcode that AVPlayerViewController stays in memory and there's a bunch of instances of it.

The key though is LET THE VIDEO PLAY UNTIL THE END where the controls pop back up.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(6)) { [weak self] in
        let assetURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!

        let player = AVPlayer(url: assetURL)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self?.navigationController?.present(playerViewController, animated: true) {
            playerViewController.player!.play()
        }
    }
}

Try this

self?.navigationController?.present(playerViewController, animated: true) { [weak playerViewController]
        playerViewController?.player!.play()
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM