简体   繁体   English

iOS在CGRect中播放电影

[英]iOS play movie in CGRect

I am trying to add an intro video into my iOS app. 我正在尝试将介绍性视频添加到我的iOS应用中。 I want the video to play with no controls and in a box I create for it. 我希望视频在没有控件的情况下播放,并在为它创建的框中播放。 but am not sure how to do that. 但不知道该怎么做。 It is also not playing at the correct size it is super small. 它也没有以超小尺寸正确播放。 If you look below you will see my attempt but it fails to work. 如果您在下面看,您会看到我的尝试,但是它没有用。 How do I achieve this? 我该如何实现?

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

在此处输入图片说明

I actually have code in my book that shows you exactly how to do this very thing: 我的书中实际上有一些代码可以向您确切演示如何执行此操作:

http://www.apeth.com/iOSBook/ch28.html#_mpmovieplayercontroller http://www.apeth.com/iOSBook/ch28.html#_mpmovieplayercontroller

Read down to the first block of code. 读到第一段代码。 As you can see, we load a movie, decide on the rect within our view where we want to display it, and display it: 如您所见,我们加载影片,在视图中的rect上决定要显示的影片,并显示它:

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

Of course you will want to change those values! 当然,您将需要更改这些值! But that's the technique. 但这就是技巧。 Also you're going to want to get rid of the controls, but that's easy (read further down in the chapter). 同样,您将想要摆脱控件,但这很容易(在本章的后续内容中进一步了解)。

Since you are using the player in a view, you don't need to use a MPMoviePlayerViewController . 由于您是在视图中使用播放器,因此不需要使用MPMoviePlayerViewController Try the following code: 尝试以下代码:

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

Also, if you are using a navigation bar or status bar, you should take that away from the height: 另外,如果您使用的是导航栏或状态栏,则应远离高度:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM