首页 > 代码库 > 利用MPMoviePlayerViewController实现简单的mp4播放
利用MPMoviePlayerViewController实现简单的mp4播放
1、要使用MPMoviePlayerViewController首先要加入MediaPlayer.framework.
2、加入如下代码:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad { [super viewDidLoad]; NSString *url = [[NSBundle mainBundle] pathForResource:@"TaylorSwift-LoveStory" ofType:@"mp4"]; MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:[playerViewController moviePlayer]]; //-- add to view--- [self.view addSubview:playerViewController.view]; //---play movie--- MPMoviePlayerController *player = [playerViewController moviePlayer]; [player play]; }- (void) movieFinishedCallback:(NSNotification*) aNotification { MPMoviePlayerController *player = [aNotification object]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player]; [player stop]; [self.view removeFromSuperView]; [player autorelease];}
3、支持横屏修改shouldAutorotateToInterfaceOrientation:interfaceOrientation方法使其返回YES。
备注:实测 用上面的方法直接播放本地m3u8格式或者线上的视频也是可以的。
NSURL可直接初始化:
NSURL*videoPathURL=[NSURL URLWithString:urlStr];//urlStr是视频播放地址
如果是播放本地视频的话。这样初始化:
NSURL*videoPathURL=[[NSURL alloc] initFileURLWithPath:urlStr];
转:http://re-reference.iteye.com/blog/1106324
利用MPMoviePlayerViewController实现简单的mp4播放
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。