0

我正在尝试将介绍视频添加到我的 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];
}

在此处输入图像描述

4

2 回答 2

1

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

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:

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).

于 2013-05-01T03:44:49.450 回答
0

由于您在视图中使用播放器,因此不需要使用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];
于 2013-05-01T00:27:25.243 回答