我想在纵向模式下在启动画面中播放一些视频 - 全屏。那可能吗?请引导我通过适当的解决方案。
我正在寻找快速的解决方案
使用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
:假冒闪屏,主屏幕等等......
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()
}
如果在此有任何问题,请告诉我
应用委托内部的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()
}