Search code examples
swiftiphoneios-simulatorios13ios12

AVPlayerViewController Video Worked on iOS 11, 12 but Not iOS 13 With iPhone 11


I'm receiving a strange bug where my background video plays as expected on iOS 13 iPhone 8, iPhone 8+, iPhone 11 simulator (and device) but does NOT play on iOS 13 iPhone 11 Pro or iPhone 11 Pro Max simulator.

I reference the VideoSplashViewController below in a separate landing page view controller.

VideoSplashViewController

import UIKit
import MediaPlayer
import AVKit

public enum ScalingMode {
  case resize
  case resizeAspect
  case resizeAspectFill
}

open class VideoSplashViewController: UIViewController {

  fileprivate let moviePlayer = AVPlayerViewController()
  fileprivate var moviePlayerSoundLevel: Float = 1.0
  open var contentURL: URL? {
    didSet {
      setMoviePlayer(contentURL!)
    }
  }

  open var videoFrame: CGRect = CGRect()
  open var startTime: CGFloat = 0.0
  open var duration: CGFloat = 0.0
  open var backgroundColor: UIColor = UIColor.white {
    didSet {
      view.backgroundColor = backgroundColor
    }
  }
  open var sound: Bool = true {
    didSet {
      if sound {
        moviePlayerSoundLevel = 1.0
      }else{
        moviePlayerSoundLevel = 0.0
      }
    }
  }
  open var alpha: CGFloat = CGFloat() {
    didSet {
      moviePlayer.view.alpha = alpha
    }
  }
  open var alwaysRepeat: Bool = true {
    didSet {
      if alwaysRepeat {
        NotificationCenter.default.addObserver(self,
          selector: #selector(VideoSplashViewController.playerItemDidReachEnd),
          name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
          object: moviePlayer.player?.currentItem)
      }
    }
  }
  open var fillMode: ScalingMode = .resizeAspectFill {
    didSet {
      switch fillMode {
      case .resize:
        moviePlayer.videoGravity = AVLayerVideoGravity(rawValue: AVLayerVideoGravity.resize.rawValue)
      case .resizeAspect:
        moviePlayer.videoGravity = AVLayerVideoGravity(rawValue: AVLayerVideoGravity.resizeAspect.rawValue)
      case .resizeAspectFill:
        moviePlayer.videoGravity = AVLayerVideoGravity(rawValue: AVLayerVideoGravity.resizeAspectFill.rawValue)
      }
    }
  }

  override open func viewDidAppear(_ animated: Bool) {

  }

    var coverView = UIView()

    override open func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)


        view.backgroundColor = UIColor.white

        moviePlayer.view.frame = videoFrame
        moviePlayer.view.backgroundColor = UIColor.white

        moviePlayer.showsPlaybackControls = false
        view.addSubview(moviePlayer.view)
        view.sendSubviewToBack(moviePlayer.view)

        coverView.frame = CGRect.init(x: 0, y: 0, width: 500, height: 500)
        coverView.backgroundColor = UIColor.white
        coverView.layer.zPosition = 11;
        coverView.isUserInteractionEnabled = false
        moviePlayer.view.addSubview(coverView)

        UIView.animate(withDuration: 2) {
            self.coverView.backgroundColor = UIColor(hue: 0.5194, saturation: 0.4, brightness: 0.94, alpha: 0.1)
            }

        _ = try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: .default, options: .mixWithOthers)

    }

  override open func viewWillDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    NotificationCenter.default.removeObserver(self)
    moviePlayer.view.backgroundColor = UIColor.white
  }

  fileprivate func setMoviePlayer(_ url: URL){
    let videoCutter = VideoCutter()
    videoCutter.cropVideoWithUrl(videoUrl: url, startTime: startTime, duration: duration) { (videoPath, error) -> Void in
      if let path = videoPath as URL? {
        DispatchQueue.global().async {
          DispatchQueue.main.async {
            self.moviePlayer.player = AVPlayer(url: path)
            self.moviePlayer.player?.play()
            self.moviePlayer.player?.volume = self.moviePlayerSoundLevel
            //added to mute
            self.moviePlayer.player?.isMuted = true
          }
        }
      }
    }
  }

  override open func viewDidLoad() {
    super.viewDidLoad()
    moviePlayer.view.backgroundColor = UIColor.white
  }

  override open func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
  }

  @objc func playerItemDidReachEnd() {
    moviePlayer.player?.seek(to: CMTime.zero)
    moviePlayer.player?.play()
  }
}

This is the code called on my main landing page view.

Code Called In Separate VideoSplashViewController inherited VC

    func setupVideoBackground() {
        let url = URL(fileURLWithPath: Bundle.main.path(forResource: "timelapse_clouds_compress", ofType: "mp4")!)

        // setup layout
        videoFrame = self.view.frame
        fillMode = .resizeAspectFill
        alwaysRepeat = true
        sound = true
        startTime = 2.0
        alpha = 0.8

        contentURL = url
        view.isUserInteractionEnabled = true
    }

Part of me suspects this is a simulator issue because I see no alerts or errors in XCode and the video works as expected on a real iPhone 8+ iOS 13. Will try to test on a real iPhone 11 Pro Max soon but would appreciate any help!


Solution

  • The video works well on an actual iPhone 11 Pro and iPhone 11 Pro Max device. This is a simulator bug.