Reputation: 1818
I want to play some video in splash screen in portrait mode - full screen. Is that possible? please guide me through a proper solution.
I'm looking for swift solution
Upvotes: 4
Views: 8861
Reputation: 10105
create a subclass of UIView
, with a AVPlayer
and a function createBackground
:
import AVFoundation
class BackgroundVideo: UIView {
var player: AVPlayer?
func createBackground(name: String, type: String) { }
}
then your createBackground
might be something like:
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 = AVPlayer.ActionAtItemEnd.none
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.frame
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
self.layer.insertSublayer(playerLayer, at: 0)
player?.seek(to: CMTime.zero)
player?.play()
}
then you may add more stuff, such as observing notifications:
AVPlayerItemDidPlayToEndTime
for implementing a video loop.UIApplicationWillEnterForeground
for controlling the video loop, after resuming from background.finally you might attach this BackgroundView
, wherever you need: fake splash screen, home screen, and so on...
Upvotes: 9
Reputation: 452
Inside didFinishLaunch method of app delegate
let splashScreenVC = viewController(withIdentifier: "identifier", inStoryboard: "storyboard:) as? SplashViewController
window?.rootViewController = splashScreenVC
Inside the view controller SplashViewController, you can play video in viewDidLoad method.
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()
}
Upvotes: 0
Reputation: 936
SWIFT 3.0
You can not do that in default splash screen but you can do some workaround to acheive this.
First of all you should take first frame of your video which will be image.
You can do that using photoshop or by any other graphics tool.
Now you can set it to you default splash screen in UIImageView.
Now make a UIViewController and launch that as initial view controller.
And below is the code to play video
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()
}
Let me know if having any trouble in this
Upvotes: 2