方法一:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//利用自带MPMoviePlayerController来实现视频播放,首先要在 项目中导入MediaPlayer.Framework框架包.
//在试图控制器中导入#import "MediaPlayer/MPMoviePlayerController.h"
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
imageView.image = [UIImage imageNamed:@"00013"];
[self.view addSubview:imageView];
[imageView release]; // 播放视频按钮
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeSystem];
playButton.frame = CGRectMake(200, 30, 100, 30);
[playButton addTarget:self action:@selector(playClick:) forControlEvents:UIControlEventTouchUpInside];
[playButton setTitle:@"播放视频" forState:UIControlStateNormal]; playButton.backgroundColor = [UIColor greenColor];
playButton.layer.cornerRadius = 5;
playButton.layer.masksToBounds = YES;
[self.view addSubview:playButton];
} - (void)playClick:(UIButton *)btn
{
//视频文件路径,此视频已经存入项目包中.属于本地播放
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];
//视频URL
NSURL *url = [NSURL fileURLWithPath:path];
//视频播放对象
MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
movie.controlStyle = MPMovieControlStyleFullscreen;
[movie.view setFrame:self.view.bounds];
movie.initialPlaybackTime = -1;
[self.view addSubview:movie.view];
//注冊一个播放结束的通知, 当播放结束时, 监听到而且做一些处理
//播放器自带有播放通知的功能, 在此只只须要注冊观察者监听通知的就可以
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification
object:movie];
[movie play];
} - (void)myMovieFinishedCallback:(NSNotification *)notify
{
//视频播放对象
MPMoviePlayerController *theMovie = [notify object];
//销毁播放通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:(theMovie)];
//释放视频对象
[theMovie.view release];
} 方法二:
- (void)viewDidLoad
{
//首先要在 项目中导入MediaPlayer.Framework框架包.
//在试图控制器中导入#import <MediaPlayer/MediaPlayer.h>
[super viewDidLoad];
// Do any additional setup after loading the view.
NSURL *videoURL;
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];
//推断是网络地址还是本地播放地址
if ([path hasPrefix:@"http://"]) {
videoURL = [NSURL URLWithString:path];
}else{
videoURL = [NSURL fileURLWithPath:path];
}
MPMoviePlayerViewController *_moviePlayerController= [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[_moviePlayerController.view setFrame:CGRectMake(0,100,320,200)];
_moviePlayerController.moviePlayer.movieSourceType=MPMovieSourceTypeFile;
[_moviePlayerController.moviePlayer setScalingMode:MPMovieScalingModeNone];
[_moviePlayerController.moviePlayer setRepeatMode:MPMovieRepeatModeNone];
[_moviePlayerController.moviePlayer setControlStyle:MPMovieControlModeVolumeOnly];
[_moviePlayerController.moviePlayer setFullscreen:NO animated:YES];
[_moviePlayerController.moviePlayer play];
//视频播放组件的容器,加这个容器是为了兼容iOS6,假设不加容器在iOS7以下没有不论什么问题,假设在iOS6以下视频的播放画面会自己主动铺满self.view;
UIView *moviePlayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
[self.view addSubview:moviePlayView];
[moviePlayView addSubview:[_moviePlayerController.moviePlayer view]];
}

最新文章

  1. 自定义RatingBar的一个问题(只显示显示一个星星)
  2. 【超级干货】手机移动端WEB资源整合
  3. nginx+tomcat集群配置(3)---获取真实客户端IP
  4. Count Colour_poj2777(线段树+位)
  5. Rendering Path
  6. Web交互设计优化的简易check list
  7. __declspec(dllexport) &amp; __declspec(dllimport)
  8. 红帽中出现”This system is not registered with RHN”的解决方案
  9. TEXT文本编辑框3 点击按钮添加文本至文本输入框
  10. 在springmvc中配置jedis(转)
  11. 4.SSM整合_多表_多对多的增删改查
  12. upgrade openssl
  13. &lt;转载&gt;XML操作
  14. 四、HTTP控制器
  15. 09 Zabbix4.0系统clone、mass update使用
  16. 函数后面的const修饰符的作用
  17. Tsql2008查询性能优化第一章---APPLY
  18. 【转】Fiddler工作原理
  19. 6款漂亮HTML CSS样式用户留言表单
  20. 装饰者模式——(head first 设计模式3)

热门文章

  1. vs2010 视图 aspx页面设计窗口创建控件时出错 未将对象引用设置到对象的实例
  2. [ HNOI 2006 ] 公路修建问题
  3. UDP网络程序实例
  4. 把多种验证规则用到一个model上
  5. Hash二次探测
  6. suse 下的gcc安装
  7. JS——绑定自定义属性
  8. 剔除重复jar包,查找重复类
  9. [文章转载]-Java后端,应该日常翻看的中文技术网站 -江南白衣
  10. 扩增子图表解读1箱线图:Alpha多样性