user3307029 user3307029 - 10 months ago 82
iOS Question

AVPlayer Crashes on Device When Dismissing View

I'm having an issue with the AVPlayer. I've tried many solutions but it still crashes after I move to a different view.

class GuideVideo : BaseViewController{

var avPlayer: AVPlayer?
var avPlayerLayer: AVPlayerLayer?

override func viewDidLoad() {

func generateVideo () {
let videoURLWithPath = data["VideoUrl"]
let videoFilePath = NSURL(string: videoURLWithPath!)

let avAsset: AVAsset = AVAsset.assetWithURL(videoFilePath) as! AVAsset
let avPlayerItem = AVPlayerItem(asset: avAsset)
avPlayer = AVPlayer(playerItem: avPlayerItem)
avPlayerLayer = AVPlayerLayer(player: avPlayer)

avPlayerLayer!.frame = self.videoView.bounds

I've also tried removing the observers from it since I assume the crash is related to a nil observer.

override func viewWillDisappear(animated: Bool) {
if self.avPlayerLayer != nil {
self.avPlayerLayer = nil
self.avPlayer = AVPlayer()

Nothing works and the crash provides no data. Either it crashes without indicating a line or a general

* Import thing to note is that this crash only happens on the iPhone 6/6+. Our iPhone 5C handles the class well.
* I only get the crash after moving to another view controller or a different navigation stack, but a few seconds after the view had been dismissed.

Thank you, been sitting on this for the better part of 2 days now.

EDIT: The issue is apparently related to the SWReveal. It deallocates instances before their lifecycle is over.
Accepted the best solution, but the problem is related to SWReveal.

Answer Source
  1. Try using the MPMoviePlayerController instead of the AVPlayer, for simple solutions, the MPMoviePlayerController has an easier API.
  2. make the MPMoviePlayerController a global instance making it accessible through the whole app, and then initialize it when you need it.
  3. Try playing the videos locally first and check if it solves the problem