我有一个通用应用程序,可以播放互联网上的电影。它必须支持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中看到可见的正在播放的电影,但是它不是“全屏”的,因此它没有“完成”按钮,因此我无法关闭屏幕。我很乐意为您提供帮助。谢谢!
最佳答案
我能够找到一个可以接受的解决方案,但是我仍然觉得这可能是一个错误。
如果我跳过对setFullScreen
的调用,而只是手动将controlStyle
设置为MPMovieControlStyleFullScreen
,那么它给我的视图基本上是正确的(工具栏太低了40像素)。
然后,我可以得到“完成”按钮,它会触发moviePlayer:didFinishPlaying
回调。
因此,我现在的代码中有一个臭味很大的if 3.2
逻辑分支,这很令人讨厌,但是希望大多数人仍然会使用4.0。
关于iphone - iPad 3.2和[MPMoviePlayerController setFullScreen:]未显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5629079/