iOS在CGRect中播放电影

iOS在CGRect中播放电影,ios,mpmovieplayercontroller,Ios,Mpmovieplayercontroller,我正在尝试将介绍视频添加到我的iOS应用程序中。我希望视频在没有控件的情况下播放,并在我为其创建的框中播放。但我不知道该怎么做。它的尺寸也不合适,因为它太小了。如果你往下看,你会看到我的尝试,但失败了。我如何做到这一点 -(void)initVideo{ MPMoviePlayerViewController *moviePlayerController=[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileU

我正在尝试将介绍视频添加到我的iOS应用程序中。我希望视频在没有控件的情况下播放,并在我为其创建的框中播放。但我不知道该怎么做。它的尺寸也不合适,因为它太小了。如果你往下看,你会看到我的尝试,但失败了。我如何做到这一点

-(void)initVideo{
    MPMoviePlayerViewController *moviePlayerController=[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"video" ofType:@"mp4"]]];

    UIView * contain = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [contain addSubview:moviePlayerController.view];
    [self.view addSubview:contain];

    MPMoviePlayerController *player = [moviePlayerController moviePlayer];

    player.fullscreen = NO;
    [player play];
}

由于您在视图中使用播放器,因此不需要使用
MPMoviePlayerViewController
。请尝试以下代码:

-(void)initVideo{
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"video" ofType:@"mp4"]]];

    UIView *contain = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    moviePlayerController.view.frame = contain.bounds;
    [contain addSubview:moviePlayerController.view];
    [self.view addSubview:contain];

    moviePlayerController.fullscreen = NO;
    [moviePlayerController play];
}
此外,如果您使用的是导航栏或状态栏,则应将其从高度中移除:

CGRect f = [[UIScreen mainScreen] bounds];
f.size.height -= [[UIApplication sharedApplication] statusBarFrame].size.height + self.navigationController.navigationBar.frame.size.height;
UIView *contain = [[UIView alloc] initWithFrame:f];

实际上,我的书中有一段代码,告诉你如何做这件事:

读到第一段代码。如您所见,我们加载一部电影,在视图中确定要显示它的矩形,然后显示它:

NSURL* m = [[NSBundle mainBundle] URLForResource:@"ElMirage"
                                   withExtension:@"mp4"];
MPMoviePlayerController* mp =
    [[MPMoviePlayerController alloc] initWithContentURL:m];
self.mpc = mp; // retain policy
self.mpc.shouldAutoplay = NO;
[self.mpc prepareToPlay];
self.mpc.view.frame = CGRectMake(10, 10, 300, 250);
self.mpc.backgroundView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.mpc.view];
当然,您需要更改这些值!但这就是技巧。此外,您还希望摆脱这些控件,但这很容易(请阅读本章的后续内容)。

:CGContextSaveGState:invalid context 0x0