我有一个通用的应用程序,可以在网上播放电影。它必须支持3.1.x和4.x。
为了使它能够工作,我在代码中有一个分支,用于检测预3.2设备,并使用MPMoviePlayerController,因为它应该在那里工作。
这是我让玩家准备播放远程电影的方式:
- (void)registerForMovieNotifications {
//for 3.2 devices and above
if ([moviePlayer respondsToSelector:@selector(loadState)]) {
LOG(@"moviePlayer responds to loadState, this is a 3.2+ device");
//register the notification that the movie is ready to play
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didExitFullScreen:)
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
LOG(@"preparing moviePlayer...");
[moviePlayer prepareToPlay];
} else {
//for pre-3.2 devices
LOG(@"This is a 3.1.x device");
//register the notification that the movie is ready to play
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];
}
//handle when the movie finished
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void)readyPlayer {
if (!moviePlayer) {
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
} else {
[moviePlayer setContentURL:movieURL];
}
[self registerForMovieNotifications];
}稍后我得到这个通知,它设置电影播放器的视图,等等。
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification {
LOG(@"3.2/4.x - moviePlayerLoadStateChanged:");
//unless state is unknown, start playback
if ([moviePlayer loadState] != MPMovieLoadStateUnknown) {
//remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
//set the frame of the movie player to match
self.view.autoresizesSubviews = YES;
[[moviePlayer view] setFrame:self.view.bounds];
[[moviePlayer view] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[[moviePlayer view] setAutoresizesSubviews:YES];
//add movie player as a subview
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES];
//play the movie
[moviePlayer play];
}
}还有电影剧本。这在iPhone 4.2、4.3、iPad 4.2、4.3上非常有效,但在iPad 3.2上却失败了。这部电影播放,但我得到的只是一个黑色的屏幕。
如果我删除[moviePlayer setFullscreen:YES]调用,我会在3.2中得到一个可见的播放电影,但是它不是“全屏”,因此它没有Done按钮,我也无法拒绝屏幕。
我希望能帮助了解这里发生的事情。谢谢!
发布于 2011-04-14 05:23:46
我找到了一个可以接受的解决方案,但我仍然觉得这可能是个错误。
如果我跳过对setFullScreen的调用,而只是手动将controlStyle设置为MPMovieControlStyleFullScreen,那么它将给出一个大致正确的视图(工具栏大约是40像素,太低了)。
然后,我可以得到“完成”按钮,这将触发moviePlayer:didFinishPlaying回调。
因此,我现在的代码中有一个难闻的if 3.2分支逻辑,但希望大多数人都能在4.0上使用。
https://stackoverflow.com/questions/5629079
复制相似问题