好的,在iOS中模拟启动视频的选项很少。我们所能做的就是等到应用程序完全加载,然后创建Media Player并在其中加载视频。
我用以下代码实现了它:
-(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。但是这段代码做了一些与众不同的事情,我想看看:
如你所知,我不喜欢黑屏暂停 - 它看起来很难看。
据我在控制台日志中看到的问题是,媒体播放器正在等待主视图控制器完全加载。
关于主视图的几句话:我正在为iPad编写应用程序,主视图包含多个带有多个图像的子视图。主视图中的每个图像和每个子视图都通过ASIHTTPRequest lib从Internet Web服务加载一些数据。
我认为Media Player正在等待所有初始连接完成,然后才开始播放视频......
如何在加载主视图之前强制播放视频?或者我可以延迟加载主XIB?
答案 0 :(得分:13)
你无法摆脱静态飞溅图像。虽然它显示,操作系统正在加载应用程序并实例化东西,直到它准备好调用你的UIApplicationDelegate。所以你所能做的就是使用不闪光(黑屏几秒钟)或者让你的电影完全按照显示的闪屏开始,这样看起来静态图像会突然变得有生命。
要在电影加载时摆脱黑屏,您可以尝试make the player transparent并在显示启动图像的播放器后面放置一个UIImageView。行为就是这样:
至少在理论上,这应该会导致静态图像突然开始动画效果。
但是如果你根本不使用启动画面(许多游戏都这样做),那么电影播放器一开始就显示黑屏并不重要,你不会注意到。
关于在UIImageView中显示启动画面:遗憾的是,you have to test the interface rotation and load the image manually,无法查询显示的启动画面。如果你只支持一个界面方向(再次,许多游戏都这样做),你当然没有这个问题。
答案 1 :(得分:10)
现在有一个更好的解决方案,假设您使用的是UIViewControllers。
使用MPMoviePlayerViewController而不是使用MPMoviePlayerController。以下是一些示例代码,改编自问题:
- (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];
}
答案 2 :(得分:0)
在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()
}