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];//添加
通過以上方法,就可以把視頻添加到視圖。