iOS 视频播放MPMoviePlayerController

视频

一般用MPMoviePlayerController(需要导入MediaPlayer.Framework),还有一种是使用AVPlayer.但在iOS9,苹果将要弃用MPMoviePlayerController,所以推荐使用AVPlayer.

注: 区别:MPMoviePlayerController,简单易用,通过通知监听状态,添加到View上;
AVPlayer复杂,灵活,需要自定义UI实现,需要添加到layer上;

MPMoviePlayerController

一.基本播放视频

//懒加载
- (MPMoviePlayerViewController *)playerVc
{
    if (_playerVc == nil) {
      NSURL *url = [NSURL URLWithString:self.movie.videourl];
    _playerVc = [[JDMMoviePlayerViewController alloc] initWithContentURL:url];
    _playerVc.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
    _playerVc.moviePlayer.controlStyle = MPMovieControlStyleDefault ;
    [_playerVc.moviePlayer prepareToPlay];
    [_playerVc.moviePlayer  play];
       
    }
    return _playerVc;
}


//设置数据
  self.playerVc.view.frame = CGRectMake(0, 0, JDMScreenW, _playMovieView.height);
  [self.scrollView addSubview: self.playerVc.view];
  [[UIApplication sharedApplication] setStatusBarHidden:NO];
    

二.MPMoviePlayerController全屏模式下横屏与竖屏切换

  • 1.都在AppDelegate文件中实现
    AppDelegate.h文件中引入MediaPlayer头文件
#import 
  • 2.声明记录当前应用状态变量
@implementation AppDelegate
{
     BOOL _isFullScreen;
}
  • 3.在application:didFinishLaunchingWithOptions:中注册进入全屏和退出全屏消息事件
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willEnterFullScreen:)
name:MPMoviePlayerWillEnterFullscreenNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self  
selector:@selector(willExitFullScreen:)     
name: MPMoviePlayerWillExitFullscreenNotification                                                object:nil];
                                                                                                                                               
  • 4.实现上面消息对应的事件响应函数
 - (void)willEnterFullScreen:(NSNotification *)notification
 {
     _isFullScreen = YES;
 }
 
- (void)willExitFullScreen:(NSNotification *)notification
 {
     _isFullScreen = NO;
 }
  • 5.实现application:supportedInterfaceOrientationsForWindow:方法
 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
 {
    if (_isFullScreen) {
        return UIInterfaceOrientationMaskPortrait |    UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
 } else {
     return UIInterfaceOrientationMaskPortrait;
 }
 

注意:MPMoviePlayerController在全屏播放的时候会默认隐藏状态栏,所以需要在全屏结束的时候代码手动显示状态栏,否则导航栏会出现20点的空白

  • 1.开始播放的时候
[self.scrollView addSubview: self.playerVc.view];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
  • 2.结束全屏的时候
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willExitFullScreen) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
     
     -(void)willExitFullScreen
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}

你可能感兴趣的:(iOS 视频播放MPMoviePlayerController)