如何在纵向模式下以全屏大小播放视频?

时间:2017-12-18 13:21:38

标签: ios swift avfoundation

我想以纵向模式在启动画面中播放一些视频 - 全屏。 那可能吗?请引导我通过适当的解决方案。

我正在寻找快速解决方案

3 个答案:

答案 0 :(得分:6)

创建UIView的子类,其中包含AVPlayer和函数createBackground

import AVFoundation

class BackgroundVideo: UIView {
  var player: AVPlayer?

  func createBackground(name: String, type: String) { }
}

然后你的createBackground可能是这样的:

func createBackground(name: String, type: String) {
  guard let path = Bundle.main.path(forResource: name, ofType: type) else { return }

  player = AVPlayer(url: URL(fileURLWithPath: path))
  player?.actionAtItemEnd = AVPlayerActionAtItemEnd.none;
  let playerLayer = AVPlayerLayer(player: player)
  playerLayer.frame = self.frame
  playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
  self.layer.insertSublayer(playerLayer, at: 0)
  player?.seek(to: kCMTimeZero)
  player?.play()
}

然后您可以添加更多内容,例如观察通知:

  • AVPlayerItemDidPlayToEndTime用于实施视频循环。
  • UIApplicationWillEnterForeground用于在从后台恢复后控制视频循环。

最后,您可以随时随地附上此BackgroundView:假冒闪屏,主屏幕等等......

答案 1 :(得分:1)

SWIFT 3.0

您无法在默认的初始屏幕中执行此操作,但您可以采取一些解决方法来实现此目的。

首先,您应该拍摄视频的第一帧,即图像。

你可以使用photoshop或任何其他图形工具来做到这一点。

现在您可以在UIImageView中将其设置为默认的初始屏幕。

现在创建一个UIViewController并将其作为初始视图控制器启动。

以下是播放视频的代码

    private func playFullScreenVideo() {
    // drag your video file in  project
    // check if video file is available,if not return
    guard let path = Bundle.main.path(forResource: "video", ofType:"mp4") else {
        debugPrint("video.mp4 missing")
        return
    }
    // create instance of videoPlayer with video path
    let videoPlayer = AVPlayer(url: URL(fileURLWithPath: path))
    // create instance of playerlayer with videoPlayer
    let playerLayer = AVPlayerLayer(player: videoPlayer)
    // set its videoGravity to AVLayerVideoGravityResizeAspectFill to make it full size
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
    // add it to your view
    playerLayer.frame = self.view.frame
    playerView.layer.addSublayer(playerLayer)
    // start playing video
    videoPlayer?.play()
  }

如果在此

中遇到任何问题,请告诉我

答案 2 :(得分:-2)

app appate的didFinishLaunch方法

let splashScreenVC = viewController(withIdentifier: "identifier", inStoryboard: "storyboard:) as? SplashViewController
window?.rootViewController = splashScreenVC 

在视图控制器SplashViewController中,您可以在viewDidLoad方法中播放视频。

 func playVideo() {
    guard let videoPath = Bundle.main.path(forResource: "Redtaxi-splash", ofType:"mov") else {
        return
    }

    let videoURL = URL(fileURLWithPath: videoPath)
    let player = AVPlayer(url: videoURL)
    playerViewController = AVPlayerViewController()
    playerViewController?.player = player
    playerViewController?.showsPlaybackControls = false
    playerViewController?.view.frame = view.frame
    playerViewController?.view.backgroundColor = .white
    playerViewController?.view.contentMode = .scaleAspectFill


    NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying(note:)),
                                           name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)

    view.addSubview((playerViewController?.view)!)
    playerViewController?.player?.play()
}