MPMOVIEPLAYER在横向播放多次视频时会导致视频向下移动,导航栏消失

时间:2013-01-04 03:03:20

标签: ios mpmovieplayer

我正在编写一个嵌入视频的应用程序。如果我首先在横向模式下启动应用程序,然后启动视频,它看起来不错。然后我点击完成,然后再次播放视频。第二次视频现在向下移动到左侧。当我点击视频时,“完成”按钮不会显示。我必须将模拟器切换到纵向模式,然后再切换到横向模式,然后我可以单击完成以停止视频。如果我总是以纵向模式启动视频,然后将模拟器转为横向,它将起作用。我可以多次这样做,视频将始终正确排列。但是,如果我始终以横向模式播放应用程序,则视频将在第一次播放后每次播放时始终向下和向左移动。我浏览了许多其他的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];
}

0 个答案:

没有答案