AVPlayerViewController 视频在 iOS 11、12 上工作但在 iOS 13 上工作 iPhone 11
AVPlayerViewController Video Worked on iOS 11, 12 but Not iOS 13 With iPhone 11
我收到一个奇怪的错误,我的背景视频在 iOS 13
iPhone 8、[=45 上按预期播放=] 8+, iPhone 11 模拟器(和设备)但不能在 iOS 13
[=45= 上播放] 11 Pro 或 iPhone 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()
}
}
这是在我的主着陆页视图中调用的代码。
在单独的 VideoSplashViewController 中调用的代码继承 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
}
部分我怀疑这是一个模拟器问题,因为我在 XCode 中没有看到任何警报或错误,并且视频在真实 iPhone 8+ iOS 13 上按预期工作。很快将尝试在真实的 iPhone 11 Pro Max 上进行测试,但不胜感激!
该视频在实际 iPhone 11 Pro 和 iPhone 11 Pro Max 设备上运行良好。这是一个模拟器错误。
我收到一个奇怪的错误,我的背景视频在 iOS 13
iPhone 8、[=45 上按预期播放=] 8+, iPhone 11 模拟器(和设备)但不能在 iOS 13
[=45= 上播放] 11 Pro 或 iPhone 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()
}
}
这是在我的主着陆页视图中调用的代码。
在单独的 VideoSplashViewController 中调用的代码继承 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
}
部分我怀疑这是一个模拟器问题,因为我在 XCode 中没有看到任何警报或错误,并且视频在真实 iPhone 8+ iOS 13 上按预期工作。很快将尝试在真实的 iPhone 11 Pro Max 上进行测试,但不胜感激!
该视频在实际 iPhone 11 Pro 和 iPhone 11 Pro Max 设备上运行良好。这是一个模拟器错误。