我正在编写一个嵌入视频的应用程序。如果我先以横向模式启动应用程序,然后再启动视频,它看起来不错。然后我点击完成,然后再次播放视频。第二次视频现在向下和向左移动。当我点击视频时,完成按钮不显示。我必须将模拟器切换到纵向模式,然后再切换回横向模式,然后我可以单击完成来停止视频。如果我总是以纵向模式启动视频,然后将模拟器转为横向模式,它将起作用。我可以多次这样做,并且视频将始终正确排列。但是,如果我始终以横向模式播放应用程序,则在第一次播放后,视频将始终向下和向左移动。我经历了许多其他 mpmovieplayer 问题,并将我的代码与其他代码进行了比较。我正在使用 IOS6 和 Storyboard。我在下面复制了我的基本 MPMOVIEPLAYER 代码。我真的被困在这里
MoviePlayerViewController.m
- (void) readyPlayer
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
// For 3.2 devices and above
if ([mp respondsToSelector:@selector(loadState)])
{
// Set movie player layout
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setFullscreen:YES];
// May help to reduce latency
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
// Register to receive a notification when the movie has exit fullscreen mode.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
}
/*---------------------------------------------------------------------------
* For 3.2 and 4.x devices
*
*--------------------------------------------------------------------------*/
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification
{
// Unless state is unknown, start playback
if ([mp loadState] != MPMovieLoadStateUnknown)
{
// Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
// When tapping movie, status bar will appear, it shows up
// in portrait mode by default. Set orientation to `landscape`
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
// Rotate the view for landscape playback
[[self view] setBounds:CGRectMake(0, 0, 480, 320)];
[[self view] setCenter:CGPointMake(160, 240)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
// Set frame of movie player
[[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
} else {
// Rotate the view for landscape playback
[[self view] setBounds:CGRectMake(0, 0, 1024, 748)];
[[self view] setCenter:CGPointMake(374, 512)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
// Set frame of movie player
[[mp view] setFrame:CGRectMake(0.0, 0.0, 1024.0, 748.0)];
}
// Add movie player as subview
[[self view] addSubview:[mp view]];
// Play the movie
[mp play];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
// Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}