电影播放效果不错,但播放前会有快速黑色闪光。这是将控制风格设置为MPMovieControlStyleNone的怪癖吗?
NSString *url = [[NSBundle mainBundle] pathForResource:@"00" ofType:@"mov"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
//---play video in implicit size---
player.view.frame = CGRectMake(80, 64, 163, 246);
[self.view addSubview:player.view];
// Hide video controls
player.controlStyle = MPMovieControlStyleNone;
//---play movie---
[player play];
答案 0 :(得分:19)
我刚遇到这个问题,并通过向默认的NSNotificationCenter添加一个观察者以找出电影何时完全准备好播放,然后将视图作为子视图添加到我的主视图来修复它。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkMovieStatus:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
...
if(moviePlayer.loadState & (MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK))
{
[pageShown.view addSubview:moviePlayer.view];
[moviePlayer play];
}
答案 1 :(得分:7)
在IOS 6中,mpmoviewplayer添加了一个新属性: readyForDisplay
这就是我正在玩的东西,到目前为止一直很好:
等待displayState更改并在准备好后显示视频控制器:
- (void)moviePlayerPlayState:(NSNotification *)noti {
if (noti.object == self.movieController) {
MPMoviePlaybackState reason = self.movieController.playbackState;
if (reason==MPMoviePlaybackStatePlaying) {
[[NSNotificationCenter defaultCenter] removeObserver:self name: MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
while (self.movieController.view.hidden)
{
NSLog(@"not ready");
if (self.movieController.readyForDisplay) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
NSLog(@"show");
self.movieController.view.hidden=NO;
});
}
usleep(50);
}
});
}
}
}
当播放状态变为 MPMoviePlaybackStatePlaying 时,我们开始检查readyDisplayState是否要更改。
答案 2 :(得分:6)
显然电影中有黑色闪光直到足够的电影加载,因此它可以开始播放。这是我的解决方法:
创建一个UIImageView并将MPMoviePlayerController放入其中。这样你就可以将alpha设置为0.
一旦你打[玩家玩];播放视频,设置.5秒计时器。
时间结束后,将alpha更改为1.
这将使玩家隐藏1/2秒(隐藏黑色闪光)。
答案 3 :(得分:3)
创建没有addSubview的视频并播放说明:
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(80, 64, 163, 246)];
moviePlayerController.controlStyle = MPMovieControlStyleNone;
准备要播放的视频并添加通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkMovieStatus:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[moviePlayerController prepareToPlay];
使用addSubview创建函数checkMovieStatus并播放说明:
- (void)checkMovieStatus:(NSNotification *)notification {
if(moviePlayerController.loadState & (MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK)) {
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];
}
}
答案 4 :(得分:2)
或者只是改变视图的颜色,这就是你实际看到的......
player.view.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0];
答案 5 :(得分:2)
我认为黑色闪光可能与movieSourceType
的{{1}}属性相关。
如果你没有设置它,它默认为MPMoviePlayerController
,这会导致UI被延迟,直到文件被加载。
尝试添加此行:
MPMovieSourceTypeUnknown
初始化播放器后。
答案 6 :(得分:1)
要避免黑色闪光,请使用MPMoviePlayerViewController而不是MPMoviePlayerController。我认为这个类在视图显示上创建背景,而不是在视频加载上(如MPMoviePlayerController那样)。
在将moviePlayerViewController.moviePlayer.view添加到显示视图之前,您必须将一个白色子视图(或适合您内容的子视图)添加到backgroundView,如下所示:
[moviePlayerViewController.moviePlayer.view setFrame:[displayView bounds]];
UIView *movieBackgroundView = [[UIView alloc] initWithFrame:[displayView bounds]];
movieBackgroundView.backgroundColor = [UIColor whiteColor];
[moviePlayerViewController.moviePlayer.backgroundView addSubview:movieBackgroundView];
[movieBackgroundView release];
答案 7 :(得分:1)
喜欢这里的解决方案http://joris.kluivers.nl/blog/2010/01/04/mpmovieplayercontroller-handle-with-care/ 来自iOS 6 你需要使用[self.moviePlayer prepareToPlay];并捕获MPMoviePlayerReadyForDisplayDidChangeNotification以使用[self.moviePlayer play];