ios应用程序的视频启动画面
Posted
技术标签:
【中文标题】ios应用程序的视频启动画面【英文标题】:Video Splash Screen for ios application 【发布时间】:2013-11-07 21:20:24 【问题描述】:我有一个客户想要有一个常规的图片启动屏幕,然后是一个视频启动屏幕,然后进入应用程序本身。他从他看到的另一个应用程序中得到了这个想法。我在下面的链接中记录了应用程序的介绍及其。
它有一个图片启动屏幕,然后是一个没有控件且没有用户与视频交互的视频,完成后关闭视频并进入应用程序本身。
http://www.jportdev.com/MISC/Sample.mov
图像启动画面我没有问题,但在完成后加载视频是我遇到问题的地方。
我作为 POC 来做这件事,然后在实际的应用程序中实现它。我认为所有这些都必须在 appDelegate 中完成......所以这就是我目前所拥有的。
#import "AppDelegate.h"
#import <MediaPlayer/MediaPlayer.h>
#import "ViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[ViewController alloc] init];
[self.window makeKeyAndVisible];
//original code generated by xcode
//self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
//self.window.rootViewController = self.viewController;
[self showIntro];
return YES;
-(void)showIntro
UIImage *splashImage = [UIImage imageNamed:@"splash.jpg"];
UIImageView *splashImageView = [[UIImageView alloc] initWithImage:splashImage];
splashImageView.contentMode = UIViewContentModeScaleAspectFit;
[self.window.rootViewController.view addSubview:splashImageView];
[self.window.rootViewController.view bringSubviewToFront:splashImageView];
[UIView animateWithDuration:1.5f
delay:2.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^
splashImageView.alpha = .0f;
CGFloat x = -60.0f;
CGFloat y = -120.0f;
splashImageView.frame = CGRectMake(x,
y,
splashImageView.frame.size.width-2*x,
splashImageView.frame.size.height-2*y);
completion:^(BOOL finished)
if (finished)
[splashImageView removeFromSuperview];
//calling the movie after dismissing image splash
[self showMovieIntro];
];
-(void)showMovieIntro
MPMoviePlayerViewController *playerVC=[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Test" ofType:@"mp4"]]];
MPMoviePlayerController *player = [playerVC moviePlayer];
player.controlStyle = MPMovieControlStyleNone; //hide the controls
//this doesnt work, returns a erros passing mpmovieplayerVC as oppose to a uiview
//[self presentModalViewController:playerViewController animated:YES];
//[self.window.rootViewController.view addSubview:playerVC];
//[self.window.rootViewController.view bringSubviewToFront:playerVC.view];
//this part right here makes the app crash
[[playerVC view] setFrame:_window.bounds];
[_window addSubview: [playerVC view]];
[_window bringSubviewToFront:[playerVC view]];
[player play];
//try using animation with duration on the movie but that was a no no...
// [UIView animateWithDuration:1.5f delay:2.0f options:UIViewAnimationOptionCurveEaseOut animations:nil completion:^(BOOL finished)
// if(finished)
// [playerVC removeFromSuperView];
//
// ];
【问题讨论】:
【参考方案1】:最好尽可能快速和轻量地加载应用程序,即application: didFinishLaunchingWithOptions:
不是完成这项工作的最佳位置。我建议将视频闪屏视为您的根视图控制器并像往常一样开始播放电影。通过这种方式,您可以获得快速的应用程序加载和精简的应用程序委托
【讨论】:
【参考方案2】:我认为您正在寻找的是一个简单的 AVPlayerLayer,位于 AVFoundation.framework 下。借助此功能,您可以在应用程序启动后显示视频介绍的方式变得简单。
首先,您需要创建一个显示视频介绍的根视图控制器。将其命名为 VideoIntroViewController。将其设置为窗口的 rootViewController。在 VideoIntroController 的 viewDidLoad 部分添加以下代码行:
#import <AVFoundation/AVFoundation.h>
@interface VideoIntroViewController ()
@property (strong, nonatomic) AVPlayer *player;
@end
@implementation VideoIntroViewController
- (void)viewDidLoad
[super viewDidLoad];
NSURL *videoURL = [NSURL URLWithString:@"http://www.jportdev.com/MISC/Sample.mov"];
AVPlayer* player = [AVPlayer playerWithURL:videoURL];
AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = self.view.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
playerLayer.needsDisplayOnBoundsChange = YES;
[self.view.layer addSublayer:playerLayer];
self.view.layer.needsDisplayOnBoundsChange = YES;
self.player = player;
- (void)viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];
[self.player play];
@end
使用上面的代码,如果一切正常,视频就可以播放了。现在在您的 viewDidAppear 中开始播放视频:
[self.player play];
之后您所需要的就是监控其状态。此方法的详细说明可以在 Apple 的文档here 下找到。
希望对您有所帮助。
【讨论】:
【参考方案3】:使用 [player prepareToPlay] 并将您的 MPMoviePlayerViewController 保留在一个属性中。
【讨论】:
【参考方案4】:这是一种方式:
flashScreen = [[UIImageView alloc] initWithFrame:CGRectMake(62, 134, 800, 506)];
videoUrl = [NSURL fileURLWithPath:filePath];
UIImage *flashImage = [self screenshotFromFileURL:videoUrl];
[flashScreen setImage:flashImage];
[self.view addSubview:flashScreen]
- (UIImage *)screenshotFromFileURL:(NSURL *)url
playerItem = [AVPlayerItem playerItemWithURL:url];
AVURLAsset *asset = (AVURLAsset *)playerItem.asset;
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]
initWithAsset:asset];
CGImageRef thumb = [imageGenerator
copyCGImageAtTime:CMTimeMakeWithSeconds(3.0, 1.0)
actualTime:NULL
error:NULL];
UIImage *image = [UIImage imageWithCGImage:thumb];
return image;
【讨论】:
以上是关于ios应用程序的视频启动画面的主要内容,如果未能解决你的问题,请参考以下文章