Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

iphone - Play video by default in full screen

I am playing a video by using 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];

But requirement is that by default video should be in full screen and when I minimize it should be in above frame size.

Please help me out.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this.

#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];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...