【问题标题】:Emulating splash video in iOS application在 iOS 应用程序中模拟启动视频
【发布时间】:2011-08-13 14:59:19
【问题描述】:

好的,在 iOS 中模拟启动视频的选项很少。我们所能做的就是等到应用程序完全加载,然后创建媒体播放器并在其中加载视频。

我用以下代码实现了它:

-(void) moviePlayBackDidFinish:(NSNotification*)notification
{
    NSLog(@"Intro video stopped");
    [mMoviePlayer release];
}

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    NSURL* mMovieURL;
    NSBundle *bundle = [NSBundle mainBundle];
    if(bundle != nil)
    {
        NSString *moviePath = [bundle pathForResource:@"intro" ofType:@"mp4"];
        if (moviePath)
        {
            mMovieURL = [NSURL fileURLWithPath:moviePath];
            [mMovieURL retain];
        }
    }

    mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mMovieURL];
    [mMovieURL release];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayBackDidFinish:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:mMoviePlayer];

    mMoviePlayer.controlStyle = MPMovieControlStyleNone;
    [mMoviePlayer.backgroundView addSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Splash/background.png"]] autorelease]];
    mMoviePlayer.scalingMode = MPMovieScalingModeFill;

    [window addSubview:mMoviePlayer.view];
    [mMoviePlayer setFullscreen:YES animated:NO];

    [window makeKeyAndVisible];
    [mMoviePlayer play];

<... other stuff ...>

}

我的视频只有 1 MB。但是这段代码做了一些不同的事情,然后我想看看:

  1. 首先用户可以看到几秒钟的静态闪屏;
  2. 然后出现黑屏 1 或 2 秒。我认为这是因为媒体播放器已加载。
  3. 视频开始播放。
  4. 主界面加载。

如您所知,我不喜欢黑屏的暂停 - 它看起来很难看。

据我在控制台日志中看到的,问题是媒体播放器正在等待主视图控制器完全加载。

关于主视图的几句话:我正在为 iPad 编写一个应用程序,主视图由多个带有多个图像的子视图组成。主视图中的每个图像和每个子视图都通过 ASIHTTPRequest 库从 Internet Web 服务加载一些数据。

我认为媒体播放器正在等待所有初始连接完成,然后才开始播放视频...

如何在加载主视图之前强制播放视频?或者我可以延迟主 XIB 的加载?

【问题讨论】:

    标签: iphone ipad mpmovieplayercontroller splash-screen


    【解决方案1】:

    您无法摆脱静态飞溅图像。显示时,操作系统正在加载应用程序并实例化内容,直到它准备好调用您的 UIApplicationDelegate。因此,您所能做的就是不使用启动画面(黑屏几秒钟),或者让您的电影完全从显示的启动画面开始,这样看起来静态图像会突然变成动画。

    要在电影加载时摆脱黑屏,您可以尝试make the player transparent 并在播放器后面设置一个 UIImageView 来显示启动图像。行为将是这样的:

    • 显示初始屏幕(静态图像)。
    • 应用程序已加载。您会看到 UIImageView,也显示了启动画面。最上面是透明的电影播放器​​。
    • 电影播放器​​终于加载了招式并开始播放。

    至少在理论上,这应该会导致静态图像突然开始动画的效果。

    但如果您根本不使用启动画面(很多游戏都这样做),那么电影播放器​​一开始显示黑屏并不重要,您不会注意到。

    关于在 UIImageView 中显示启动画面:不幸的是,you have to test the interface rotation and load the image manually,无法查询显示了哪个启动画面。如果你只支持一种界面方向(同样,很多游戏都这样做)你当然不会遇到这个问题。

    【讨论】:

    • 谢谢,我认为这是最好的解决方法 :) 虽然我不明白为什么播放器要等待所有连接...
    【解决方案2】:

    假设您使用的是 UIViewControllers,现在有一个更好的解决方案。

    不要使用 MPMoviePlayerController,而是使用 MPMoviePlayerViewController。这是一些示例代码,改编自问题:

    - (void)applicationDidFinishLaunching:(UIApplication *)application {
        NSURL* mMovieURL;
        NSBundle *bundle = [NSBundle mainBundle];
        if(bundle != nil)
        {
            NSString *moviePath = [bundle pathForResource:@"intro" ofType:@"mp4"];
            if (moviePath)
            {
                mMovieURL = [NSURL fileURLWithPath:moviePath];
                [mMovieURL retain];
            }
        }
    
        mMoviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:mMovieURL];
        [mMovieURL release];
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(moviePlayBackDidFinish) 
                                                     name:MPMoviePlayerPlaybackDidFinishNotification 
                                                   object:mMoviePlayer.moviePlayer];
        mMoviePlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
        [mMoviePlayer.moviePlayer.backgroundView  addSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SplashCopy.png"]] autorelease]];
        mMoviePlayer.moviePlayer.scalingMode = MPMovieScalingModeFill;
        [window.rootViewController.view addSubview:mMoviePlayer.moviePlayer.view];
        [mMoviePlayer.moviePlayer setFullscreen:YES animated:NO];
        [mMoviePlayer.moviePlayer play];
    

    }

    【讨论】:

    • 这对我来说效果很好,除了在应用程序启动时,window.rootViewController 为 nil,因此在其上调用 addSubview 无济于事。结果,您可以听到视频,但看不到它。要解决此问题,请将 addSubview 行替换为:“window.rootViewController = moviePlayer;” ...然后在您的 moviePlayBackDidFinish 方法中,将其重新分配给您希望根视图控制器在电影完成后成为的任何内容。
    • 也为我工作。但是,我必须将 moviePlayer 视图框架设置为与 rootViewController 视图框架相匹配,以消除自动调整大小的警告消息。
    【解决方案3】:

    在 iOS 应用程序中模拟启动视频 swift4.0的这段代码

            var mMovieURL: URL?
            let bundle = Bundle.main
            if bundle != nil {
                let moviePath: String? = bundle.path(forResource: "intro", ofType: "mp4")
                if moviePath != nil {
                    mMovieURL = URL(fileURLWithPath: moviePath ?? "")
    
                }
            }
            mMoviePlayer = MPMoviePlayerController(contentURL: mMovieURL!)
            NotificationCenter.default.addObserver(self, selector: #selector(self.moviePlayBackDidFinish), name: .MPMoviePlayerPlaybackDidFinish, object: mMoviePlayer)
            mMoviePlayer.controlStyle = .none
            mMoviePlayer.backgroundView.addSubview(UIImageView(image: UIImage(named: "Splash.png")))
            mMoviePlayer.scalingMode = .fill
            window?.addSubview(mMoviePlayer.view)
            // window?.rootViewController?.view.addSubview(mMoviePlayer.view)
            mMoviePlayer.setFullscreen(true, animated: false)
            window?.makeKeyAndVisible()
            mMoviePlayer.play()
    
            return true
         }
    
         @objc func moviePlayBackDidFinish(_ notification: Notification) {
             mMoviePlayer.view.removeFromSuperview()
         }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 2020-11-22
    相关资源
    最近更新 更多