美文网首页
视频播放

视频播放

作者: 温暖C | 来源:发表于2016-06-12 19:19 被阅读154次

一、AVPlayer

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

  1. 本地视频:
#import <AVFoundation/AVFoundation.h>
@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 <AVFoundation/AVFoundation.h>
@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 <MediaPlayer/MediaPlayer.h>
@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 <MediaPlayer/MediaPlayer.h>
@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时创建的视频播放控制器不需要强引用。

相关文章

  • 初级视频播放功能

    打开相册选择视频 使用系统播放器播放视频 使用VideoView播放视频 使用SurfaceView播放视频 vo...

  • 3.4 音频播放.视频播放.相册调用.相机调用

    音频播放.视频播放.相册调用.相机调用 音频播放 视频播放 相册调用 视频音频资源 视频音频资源.png

  • 视频播放器

    系统播放器 打开视频列表 调用系统播放器播放视频 调用系统播放器播放网络视频 VideoView播放器 调用 V...

  • 视频播放

  • 视频播放

    import "ViewController.h" import "ZSPlayerView.h" @interf...

  • 视频播放

    NSString *file = [[NSBundle mainBundle]pathForResource:@"...

  • 视频播放

    视频播放器的实现有多种方式,此处主要针对主流方式实现一个播放器 MediaPlayer框架

  • 视频播放

    一. 视频播放介绍 实现方案四种: 1.AVPlayer 2.MPMoviePlayerControlle 3.M...

  • 播放视频

    主要使用 VideoView 类来实现。和 MediaPlayer 比较类似。 VideoView工作流程 1. ...

  • 视频播放

    1.依赖compile 'fm.jiecao:jiecaovideoplayer:2.0' compile'fm....

网友评论

      本文标题:视频播放

      本文链接:https://www.haomeiwen.com/subject/fboedttx.html