ViewController.h文件中添加如下代码:
#import <MediaPlayer/MediaPlayer.h>
@property(nonatomic,retain) MPMoviePlayerController *movieController;//添加movieController的属性
ViewController.m文件中添加如下代码:
@synthesize movieController;//合成movieController,自动生成set,get方法
NSURL *videoUrl = [[NSBundle mainBundle]URLForResource:@"clear" withExtension:@"mp4"];//定位资源clear.mp4
movieController = [[MPMoviePlayerController alloc]initWithContentURL:videoUrl];//初始化movieController
[movieController.view setFrame:self.view.bounds];//movieController视图的大小
[movieController setRepeatMode:MPMovieRepeatModeOne];//重复方式
[movieController setScalingMode:MPMovieScalingModeAspectFill];//缩放方式满屏
[movieController play];//播放
[self.view addSubview:movieController.view];//添加
通过以上方法,就可以把视频添加到视图。