In iOS4 for iPhone 4/3GS, I have a tableview and one of the cells plays back a movie file. If the movie finishes playing back and the controls have disappeared the view comes back in under t开发者_C百科he status bar. Like in this image...that I'm too new to post. See it here...
http://www.dezignwright.com/ios4_movie.png
If the controls are on when the movie finishes, then there is no problem.
BONUS: How do I force the movie player into landscape when it starts playing. I don't want it to play in portrait at all.
Thanks.
@Nuoji is close to the right answer. The frame of the view in question is being animated by iOS when MPMoviePlayerViewController is dismissed. Due to a bug in iOS when the playback ends automatically (when the movie ends) the animation assumes the view in question takes up the full screen when it may not.
Step 1. Save the frame of the superview containing the view that will launch MPMoviePlayerViewController.
Step 2. Attach a listener for the playbackDidFinishNotification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playbackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
Step 3. In the notification handler reset the frame back to the original but after a delay to allow the animation to finish. If the frame is modified during the animation it has no effect.
[self performSelector:@selector(resetFrame)
withObject:nil
afterDelay:kResetFrameDelay];
- (void)resetFrame {
self.view.frame = kApplicationFrame;
}
This issue has been addressed and is no longer an issue as of iOS 4.3.
Thanks for everyones input.
This appears to be a bug in 4.0, it works correctly when exiting using the "Done" button.
The workaround I use is to manually store the frame then restore it when receiving the MPMoviePlayerPlaybackDidFinishNotification
.
Finally to get it in landscape mode, use a subclass of MPMoviePlayerViewController
where you override shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
I.e. something like this:
@interface CustomMoviePlayerViewController : MPMoviePlayerViewController
@end
@implementation CustomMoviePlayerViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft;
}
@end
And in your controller to work around the bug:
- (void)playbackEnded:(NSNotification *)notification
{
[[self view] setFrame:[self originalFrame]];
}
- (void)playMovie:(NSString *)movieURLString
{
MPMoviePlayerViewController *controller = [[CustomMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:movieURLString]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackEnded:) name:MPMoviePlayerPlaybackDidFinishNotification object:[controller moviePlayer]];
[self presentMoviePlayerViewControllerAnimated:controller];
}
I worked around the problem by explicitly declaring that I don't use fullscreen layout on my original view just before I started the movie player:
-(IBAction)playMovieButtonPressed:(id)sender {
MPMoviePlayerViewController* playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[self localMovieURL]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playbackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerViewController.moviePlayer];
[self setWantsFullScreenLayout:NO];
// this ensures that the original frame is restored correctly
// if the movie ends and the player is closed automatically
[self presentMoviePlayerViewControllerAnimated:playerViewController];
[playerViewController release];
MPMoviePlayerController *player = [playerViewController moviePlayer];
[player play];
}
I had the same problem and I've used this workaroud to fix this:
-(void)myMovieFinishedCallback:(NSNotification*)aNotification
{
if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
CGRect frame = CGRectMake(0, 20, 768, 1004);
self.view.frame = frame;
}else {
CGRect frame = CGRectMake(0, 20, 1004, 768);
self.view.frame = frame;
}
}
Adjust the Rect as your screen size, my code should works for iPad.
精彩评论