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

问题描述 投票:3回答:3

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

我正在寻找快速的解决方案

ios swift avfoundation
3个回答
5
投票

使用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
投票

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
投票

应用委托内部的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()
}
© www.soinside.com 2019 - 2024. All rights reserved.