AVPlayerViewController 视频适用于 iOS 11、12,但不适用于 iPhone 11 的 iOS 13

Posted

技术标签:

【中文标题】AVPlayerViewController 视频适用于 iOS 11、12,但不适用于 iPhone 11 的 iOS 13【英文标题】:AVPlayerViewController Video Worked on iOS 11, 12 but Not iOS 13 With iPhone 11 【发布时间】:2020-02-05 09:35:20 【问题描述】:

我收到一个奇怪的错误,我的背景视频在 ios 13 iPhone 8iPhone 8+iPhone 11 上按预期播放> 模拟器(和设备),但不能在 iOS 13 iPhone 11 ProiPhone 11 Pro Max 模拟器上播放。

我在一个单独的登陆页面视图控制器中引用了下面的VideoSplashViewController

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()
  


这是在我的主要目标网页视图中调用的代码。

在继承 VC 的独立 VideoSplashViewController 中调用的代码

    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
    

我的一部分怀疑这是一个模拟器问题,因为我在 XCode 中没有看到任何警报或错误,并且视频在真正的 iPhone 8+ iOS 13 上按预期工作。很快将尝试在真正的 iPhone 11 Pro Max 上进行测试,但会感谢您的帮助!

【问题讨论】:

【参考方案1】:

该视频在实际的 iPhone 11 Pro 和 iPhone 11 Pro Max 设备上运行良好。这是一个模拟器错误。

【讨论】:

以上是关于AVPlayerViewController 视频适用于 iOS 11、12,但不适用于 iPhone 11 的 iOS 13的主要内容,如果未能解决你的问题,请参考以下文章

AVPlayerViewController 拒绝加载/播放视频文件/流

在 AVPlayerViewController 中将视频旋转 90 度 - 可能吗?

AVPlayerViewController 不播放本地视频

当 ShowsPlaybackControls 为 False 时,AVPlayerViewController 不会自动旋转视频

在 AVPlayerLayer 和 AVPlayerViewController 中同步视频

在 swift 中,如何在 AVPlayerViewController 中播放视频时检测触摸