IOS Objective C 中作为启动屏幕的视频

Video as launch screen in IOS Objective C

视频作为 IOS Objective C 中的启动屏幕。 任何帮助表示赞赏。

莫西 您可以使用 AVPlayer 和 AVPlayerViewController 帮助实现这一点。

我会详细给大家讲解一下这个技巧的制作过程。

第一步 在您的 Xcode 项目中添加以下框架并将它们导入您的 Appdelegate。

  1. @import AVFoundation;
  2. @import AVKit;
  3. @import UIKit;

第二步 在您的 Appdelegate didFinishLaunchingWithOptions 方法中添加以下代码

    // grab a local URL to our video
        NSURL *videoURL = [[NSBundle mainBundle]URLForResource:@"myvideo" withExtension:@"mov"];

        // create an AVPlayer
        AVPlayer *player = [AVPlayer playerWithURL:videoURL];

        // create a player view controller
        AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
        controller.player = player;
        controller.showsPlaybackControls = false;
        controller.view.frame = self.window.frame;
        controller.modalPresentationStyle = UIModalPresentationFullScreen;
        self.window.rootViewController = controller;
        [player play];

// Adding Observer for your video file,
        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
        [notificationCenter addObserver:self selector:@selector(videoDidFinish:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

第三步

视频完成后,您将被带到这个地方

- (void)videoDidFinish:(id)notification {   

AVPlayerItem *p = [notification object];
        //do something with player if you want

 //Remove Observer  
[[NSNotificationCenter defaultCenter] removeObserver:self];     

//your initial view can proceed from here 
[self ProccedToYourViewController];
}

一些小技巧

您可以通过添加淡入淡出动画来平滑地停止视频播放,我将向您展示我是如何做到的

//AvplayerViewController View(Video View).
[self.controller.view setAlpha:1.0f];
        [UIView animateWithDuration:2.0f animations:^{
//AvplayerViewController View(Video View) setting to hide.
            [self.controller.view setAlpha:0.0f];
//initialview(Your Initial View) that add as window root now
            self.window.rootViewController  = initialview;
            [initialview.view setAlpha:1.0f];
            [self.window makeKeyAndVisible];
} completion:nil];

Now You are ready to go. Cheers