码迷,mamicode.com
首页 > 其他好文 > 详细

利用MPMoviePlayerViewController实现简单的mp4播放

时间:2014-09-24 21:22:27      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   使用   for   div   sp   

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

bubuko.com,布布扣

利用MPMoviePlayerViewController实现简单的mp4播放

标签:style   blog   http   color   io   使用   for   div   sp   

原文地址:http://www.cnblogs.com/ygm900/p/3991359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!