我有一個通用的應用程序,從互聯網上播放電影。它必須支持3.1.x以及4.x.iPad 3.2和[MPMoviePlayerController setFullScreen:]沒有顯示
爲了讓這個工作,我有一個代碼分支,檢測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中看到一個可見的播放影片,但它不是「全屏」,因此它沒有完成按鈕,因此我無法關閉屏幕。
我想愛一些幫助在這裏發生了什麼。謝謝!
希望我有一個運行3.2的設備來測試這個給你。看起來它可能是3.2中的一個bug。你是否嘗試調用'setFullscreen:animated:'來代替,以防萬一? – 2011-04-12 15:02:00
從通知處理程序中刪除所有視圖處理。僅留下moviePlayer播放方法。早些時候做一些特定觀點的東西。 hth – Till 2011-04-12 19:02:27
@Nathan - 是的,我確實嘗試過:) – 2011-04-13 22:31:02