默认情况下全屏播放视频

时间:2011-05-12 11:35:08

标签: iphone objective-c ipad

我正在使用MPMoviePlayerController播放视频:

NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"myvideo.MOV" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0,0, 1024, 675);  
[moviePlayer play];

但要求是默认情况下视频应全屏显示,当我最小化时,视频应在帧大小以上。

请帮帮我。

1 个答案:

答案 0 :(得分:2)

试试这个。

#define degreesToRadian(x) (M_PI * (x) / 180.0)

-(void)playMovieAtURL:(NSURL*)movieURL
{
    if ([NSClassFromString(@"MPMoviePlayerController") instancesRespondToSelector:@selector(view)])
    {

 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200

        // running iOS 3.2 or better
        mViewPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
        mViewPlayer.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

        CGRect newFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        mViewPlayer.view.frame = newFrame;

        CGAffineTransform landscapeTransform;
        landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
        landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);

        [mViewPlayer.view setTransform: landscapeTransform];

        [self.view addSubview:mViewPlayer.view];

        [mViewPlayer.moviePlayer play];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieDidExitFullscreen:)
                                                                                  name:MPMoviePlayerPlaybackDidFinishNotification
                                                                                  object:[mViewPlayer moviePlayer]];
#endif
    }
    else 
    {
        MPMoviePlayerController *mMPPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

        mMPPlayer.scalingMode=MPMovieScalingModeFill;
        mMPPlayer.backgroundColor=[UIColor blackColor];

        [mMPPlayer play];

        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(moviePlayerDidFinish:)
                                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                                    object:mMPPlayer];
    }
}

/*---------------------------------------------------------------------------
 * 
 *--------------------------------------------------------------------------*/

- (void) movieDidExitFullscreen:(NSNotification*)notification 
{    
    //[[UIApplication sharedApplication] setStatusBarHidden:YES];

    /*/ Remove observer
    [[NSNotificationCenter  defaultCenter] 
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification 
     object:nil];

    [self dismissModalViewControllerAnimated:YES];*/

    [[NSNotificationCenter defaultCenter] removeObserver: self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object: [notification object]];

    MPMoviePlayerController *theMovie1 = [notification object];

    [theMovie1.view removeFromSuperview];
    [theMovie1 release];
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification 
{    
    //[[UIApplication sharedApplication] setStatusBarHidden:YES];

    /*/ Remove observer
    [[NSNotificationCenter  defaultCenter] 
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification 
     object:nil];

    [self dismissModalViewControllerAnimated:YES];*/

    [[NSNotificationCenter defaultCenter] removeObserver: self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object: [notification object]];

    MPMoviePlayerController *theMovie1 = [notification object];

    [theMovie1.view removeFromSuperview];
    [theMovie1 release];
}
相关问题