近日被问到播放器,了解到只是mp4的格式,所以就推荐使用了系统的MPPlayer,只不过在iOS9之后被废弃掉了,不过简单的使用还是可以的,虽然被废弃掉,但是像UIAlertView一样,它依旧可以使用。下面简单来看下代码实现:
首先要加入系统库:MediaPlayer.framework
引入#import
//#pragma mark - 播放视频调用的方法 - (void)playMedia { [self playMediaWithUrlString:@"http://flv.bn.netease.com/videolib3/1605/19/inorj0799/HD/inorj0799-mobile.mp4"]; } - (void)playMediaWithUrlString:(NSString *)urlString { _playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlString]]; _playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile; //这里的意思是设定播放的起始时间,以秒为单位 _playerController.moviePlayer.currentPlaybackTime=20; [self presentViewController:_playerController animated:YES completion:^{ [_playerController.moviePlayer play]; }]; // 点击Done 发送一条广播 // 通过通知中心, 注册self为这条广播的观察者 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DoneButtonClicked) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; } - (void)DoneButtonClicked { // remove观察者 [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; if (_playerController) { [_playerController.moviePlayer stop]; _playerController = nil; [_playerController.view removeFromSuperview]; } }
代码下载地址:https://github.com/codeliu6572/SystemPlayer