5

I am trying to make a short .mp4 video as a splash screen to show it when starting the app.

is there any example for that ?

MBH
  • 16,271
  • 19
  • 99
  • 149

2 Answers2

10

See the answer below by DarkDust in Emulating splash video in iOS application.

I Think one solution for you is show a Splash (IMG) and than you play the video you like.

To play videos in swift use AV Foundation https://developer.apple.com/av-foundation/

You cannot get rid of the static splash image. While it is shown, the OS is loading the application and instantiating stuff until it is ready to call your UIApplicationDelegate. So all you can do is either use no splash (black screen for a few seconds) or make your movie start exactly with the shown splash screen so it looks like the static image would suddenly animate.

To get rid of the black screen while the movie loads, you can try to make the player transparent and have an UIImageView behind the player that shows the splash image. The behavior would be this:

Splash screen is shown (static image). Application is loaded. You see the UIImageView, also showing the splash screen. On top of it is the transparent movie player. Movie player finally has loaded the move and starts playing it. At least in theory, this should cause the effect that the static image suddenly starts animating.

But if you don't use a splash screen at all (a lot of games do that), then it doesn't matter that the movie player is showing a black screen at first, you wouldn't notice.

Regarding showing the splash screen in an UIImageView: unfortunately, you have to test the interface rotation and load the image manually, there's no way to query which splash screen was shown. If you only support one interface orientation (again, a lot of games do this) you don't have this problem, of course.

Community
  • 1
  • 1
marchiore
  • 582
  • 2
  • 6
  • 21
2

You can create a custom ViewController for "Main Interface" and use it after the LaunchScreen with use AVPlayer inside. In swift will be something like this:

var player: AVPlayer?

override func viewDidLoad() {
    super.viewDidLoad()

    loadVideo()
}

private func loadVideo() {

    //this line is important to prevent background music stop
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
    } catch { }

    let path = NSBundle.mainBundle().pathForResource("your path", ofType:"mp4")

    player = AVPlayer(URL: NSURL(fileURLWithPath: path!))
    let playerLayer = AVPlayerLayer(player: player)

    playerLayer.frame = self.view.frame
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
    playerLayer.zPosition = -1

    self.view.layer.addSublayer(playerLayer)

    player?.seekToTime(kCMTimeZero)
    player?.play()
}
Ana Llera
  • 1,376
  • 16
  • 32
  • 1
    This is incorrect. You can't have any logic execute in the launch screen. In fact, this ViewController will not even be loaded. – 0x6A75616E Jun 07 '16 at 16:50
  • @thatjuan, you can't get rid of the static splash image but you can start a video on your first ViewController, visually the effect can be undistinguishable. For more info check this answer http://stackoverflow.com/a/7051256/1887908 – Ana Llera Jun 08 '16 at 09:25
  • 1
    I understand. However, your answer specifically says "You can create a ViewController for the LaunchScreen" which is not correct. – 0x6A75616E Jun 08 '16 at 21:38
  • How can I make transparent avplayer? – Nemanja Nov 01 '16 at 10:57