Reputation: 4281
I am trying to show splash screen with video. For this i have two queries :
The video i am showing on it is only showing black screen. I cant guess what i have done wrong. Below is the code for it.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let vc = SplashScreenVC(nibName: "SplashScreenVC", bundle: nil)
let rootViewController = UINavigationController(rootViewController: vc)
self.window?.rootViewController = rootViewController
self.navigationController?.navigationBarHidden = true
self.window?.makeKeyAndVisible()
// Override point for customization after application launch.
return true
}
class SplashScreenVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBarHidden = true
playVideo()
}
func playVideo() {
let moviePath = NSBundle.mainBundle().pathForResource("animationVideo", ofType: "mp4")
let movieUrl = NSURL.fileURLWithPath(moviePath!)
let moviePlayer : MPMoviePlayerController = MPMoviePlayerController(contentURL: movieUrl)
moviePlayer.prepareToPlay()
let vV = self.view.viewWithTag(SplashScreenUITags.videoView.rawValue)! as UIView
moviePlayer.controlStyle = MPMovieControlStyle.None
moviePlayer.scalingMode = MPMovieScalingMode.Fill
moviePlayer.movieSourceType = MPMovieSourceType.File;
moviePlayer.view.frame = vV.bounds
vV.addSubview(moviePlayer.view)
moviePlayer.play()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("moviePlayBackDidFinish:"), name: MPMoviePlayerPlaybackDidFinishNotification, object: moviePlayer)
}
}
Upvotes: 0
Views: 1464
Reputation: 1941
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.
Upvotes: 1
Reputation: 1916
About your second question you can use AVPlayer inside your SplashScreenVC. 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()
}
Upvotes: 1
Reputation: 4825
It is not possible to play video in launch screen. What you have to do is take first frame of your video as launch image or put inside launch screen stoyboard. Write separate view controller which plays your actual video and loaded from a storyboard. Set this story board as main interface in general tab of Xcode project.
Upvotes: 0
Reputation: 168
First of all make sure in General tab in Launch Screen File Main.storyboard is selected because you are using ViewController as splash view.
Upvotes: 0