视频播放

一、AVPlayer

能播放本地、远程的音频、视频文件;基于Layer显示,得自己去编写控制面板。

  1. 本地视频:
#import 
@interface ViewController ()
@property (nonatomic, strong) AVPlayer *player;
@end

@implementation ViewController
- (void)viewDidLoad {

    [super viewDidLoad];

    // 初始化
    NSURL*url = [[NSBundlemainBundle] URLForResource:@"promo_full.mp4"withExtension:nil];

    self.player = [AVPlayer playerWithURL:url];

    // 设置播放图层
    AVPlayerLayer *playerLayer = [AVPlayerLayer  playerLayerWithPlayer:self.player];
playerLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:playerLayer];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // 开始播放
    [self.player play];
}
@end

2.在线视频:

#import 
@interface ViewController ()
@property (nonatomic, strong) AVPlayer *player;
@end

@implementation ViewController
- (void)viewDidLoad {

    [super viewDidLoad];

    // 初始化
    NSURL*url = [NSURLURLWithString:@"http://streams.videolan.org/streams/mp4/Mr_MrsSmith-h264_aac.mp4"];

    self.player = [AVPlayer playerWithURL:url];

     // 设置播放图层
   AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:playerLayer];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // 开始播放
   [self.player play];
}
@end

注意:
播放视频时需要设置播放视频图层。

二、MPMoviePlayerController

  1. 能播放本地、远程的音频、视频文件,YouTobe就是用MPMoviePlayerController实现的

  2. 它继承自NSObject,自带播放控制面板(暂停、播放、播放进度、是否要全屏)

  3. MPMoviePlayerController可以播放的视频格式包括:H.264、MPEG-4等;支持的文件扩展名包括:avi,mkv,mov,m4v,mp4等

  4. 此类定义在了MediaPlayer框架

  5. 加载视频资源(注意,如果url为nil同样可以加载)
    NSAssert(self.url, @"URL不能为空");
    [[MPMoviePlayerController alloc] initWithContentURL:self.url];

  6. 显示 :[self.view addSubview:self.moviePlayer.view];
    通过设置AutoresizingMask属性可以在横竖屏转换时自动调整视图大小

  7. 播放:[self.moviePlayer play];

  8. 全屏 :[self.moviePlayer setFullscreen:YES animated:YES];

  9. MPMoviePlayerController的播放状态是通过通知中心监听的,常用监听通知事件

  • 状态变化
    MPMoviePlayerPlaybackStateDidChangeNotification

  • 播放结束
    MPMoviePlayerPlaybackDidFinishNotification

  • 退出全屏
    MPMoviePlayerDidExitFullscreenNotification

  • 截屏完成
    MPMoviePlayerThumbnailImageRequestDidFinishNotification

  • 截屏方法
    -requestThumbnailImagesAtTimes:timeOption:

  1. 代码实现

#import 
@interface ViewController ()
/**
 *  视频播放器
 */
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建视频播放器
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"promo_full.mp4" withExtension:nil];
    // NSURL *url = [NSURL URLWithString:@"http://streams.videolan.org/streams/mp4/Mr_MrsSmith-h264_aac.mp4"];
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    
    // 设置播放显示视图
    self.moviePlayer.view.frame = self.view.bounds;
    [self.view addSubview:self.moviePlayer.view];
    
    // 使用autoLayout适配横竖屏
    // 1.禁用translatesAutoresizingMaskIntoConstraints
    self.moviePlayer.view.translatesAutoresizingMaskIntoConstraints = NO;
    
    // 2.设置约束
    NSArray *constraintH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[moviePlayerView]-0-|" options:0 metrics:nil views:@{@"moviePlayerView" :self.moviePlayer.view}];
    NSArray *constraintV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[moviePlayerView]-0-|" options:0 metrics:nil views:@{@"moviePlayerView" :self.moviePlayer.view}];
    [self.view addConstraints:constraintH];
    [self.view addConstraints:constraintV];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   // 两种方式都是直接播放
    // 播放视频
    [self.moviePlayer play];
  
    // 准备后直接播放
    // [self.moviePlayer prepareToPlay];
}
@end

三、MPMoviePlayerViewController

  1. 能播放本地、远程的音频、视频文件
  2. 内部是封装了MPMoviePlayerController
  3. 播放界面默认就是全屏的
  4. 如果播放功能比较简单,仅仅是简单地播放远程、本地的视频文件,建议用这个
  5. 此类定义在了MediaPlayer框架
  6. 代码实现
#import 
@interface ViewController ()
@end

@implementation ViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // 创建播放控制器
    NSURL*url = [NSURLURLWithString:@"http://streams.videolan.org/streams/mp4/Mr_MrsSmith-h264_aac.mp4"];

    MPMoviePlayerViewController*moviePlayerVc = [[MPMoviePlayerViewControlleralloc] initWithContentURL:url];

    // 播放视频
    [self  presentMoviePlayerViewControllerAnimated:moviePlayerVc  ];
}
@end

注意:
使用MPMoviePlayerViewController时创建的视频播放控制器不需要强引用。

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