0

我有一个客户想要一个常规的图片启动屏幕,然后是一个视频启动屏幕,然后进入应用程序本身。他从他看到的另一个应用程序中得到了这个想法。我在下面的链接中记录了该应用程序的介绍及其。

它有一个图片启动屏幕,然后是一个没有控件且没有用户与视频交互的视频,完成后关闭视频并进入应用程序本身。

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];
//        }
//    }];
}
4

4 回答 4

3

最好尽可能快和轻量地加载应用程序,也就是说application: didFinishLaunchingWithOptions:,这不是完成这项工作的最佳场所。我建议将视频闪屏视为您的根视图控制器并像往常一样开始播放电影。通过这种方式,您可以获得快速的应用程序加载和瘦应用程序委托

于 2013-11-07T21:42:20.707 回答
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 文档中找到此方法的详细说明。

希望你会发现它有帮助。

于 2013-11-07T22:09:45.387 回答
1

使用 [player prepareToPlay] 并将您的 MPMoviePlayerViewController 保留在一个属性中。

于 2013-11-07T21:30:29.640 回答
1

这是一种方式:

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;
}
于 2014-04-02T03:22:55.650 回答