iOS之视频播放(一)AVPlayerViewController(AVKit)、AVPlayer(AVFoundation)

在iOS中,与视频相关的接口有5个,从顶层开始分别是 AVKit - AVFoundation - VideoToolbox - Core Media - Core Video

=============================================================================
AVKit中的AVPlayviewcontroller实现播放。
#import <AVFoundation/AVFoundation.h>

#import <AVKit/AVKit.h>


@interface playVC ()
@end
@implementation playVC


- (void)viewDidLoad {

    [superviewDidLoad];

    self.navigationItem.title=@"播放器页面";

    self.view.backgroundColor=[UIColorwhiteColor];

    [selfaddbackBtn];

    NSString *path = [[NSBundlemainBundle]pathForResource:@"test.mov"ofType:nil];


NSURL *url = [NSURLfileURLWithPath:path];

    AVPlayerViewController *playerVc = [[AVPlayerViewControlleralloc]init];

    playerVc.player = [[AVPlayeralloc]initWithURL:url];

    [selfpresentViewController:playerVcanimated:YEScompletion:nil];

}
 
======================
画中画效果-------只有在IPAd 上可以用
#import "ViewController.h"

#import <AVKit/AVKit.h>

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVPlayerViewControllerDelegate>

- (IBAction)playClick:(id)sender;

@property (nonatomic,strong)AVPlayerViewController *playerVc;

@end


@implementation ViewController



- (void)viewDidLoad {

    [superviewDidLoad];

    

    NSString *path = [[NSBundlemainBundle]pathForResource:@"test.mov"ofType:nil];

    

    NSURL *url = [NSURLfileURLWithPath:path];

    AVPlayerViewController *playerVc = [[AVPlayerViewControlleralloc]init];

    self.playerVc = playerVc;

    playerVc.player = [[AVPlayer alloc]initWithURL:url];//AVPlayerViewController有一个AVPlayer的属性;

    

    playerVc.delegate = self;

    //画中画

    //1.开启  2.属性支持画中画  3.iPad air2之后的新的

    playerVc.allowsPictureInPicturePlayback =YES;  

}



- (IBAction)playClick:(id)sender {

    [selfpresentViewController:self.playerVcanimated:YEScompletion:nil];

    //[self.playerVc.player play];

}



/*!

@method playerViewControllerWillStartPictureInPicture:

@param playerViewController

 The player view controller.

@abstract Delegate can implement this method to be notified when Picture in Picture will start.

 */

- (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController;{

    NSLog(@"playerViewControllerWillStartPictureInPicture");

}



/*!

@method playerViewControllerDidStartPictureInPicture:

@param playerViewController

 The player view controller.

@abstract Delegate can implement this method to be notified when Picture in Picture did start.

 */

- (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController;{

    NSLog(@"playerViewControllerDidStartPictureInPicture");

}



/*!

@method playerViewController:failedToStartPictureInPictureWithError:

@param playerViewController

 The player view controller.

@param error

 An error describing why it failed.

@abstract Delegate can implement this method to be notified when Picture in Picture failed to start.

 */

- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error;{

    NSLog(@"failedToStartPictureInPictureWithError");

}



/*!

@method playerViewControllerWillStopPictureInPicture:

@param playerViewController

 The player view controller.

@abstract Delegate can implement this method to be notified when Picture in Picture will stop.

 */

- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController;{

    NSLog(@"playerViewControllerWillStopPictureInPicture");

}



/*!

@method playerViewControllerDidStopPictureInPicture:

@param playerViewController

 The player view controller.

@abstract Delegate can implement this method to be notified when Picture in Picture did stop.

 */

- (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController;{

    NSLog(@"playerViewControllerDidStopPictureInPicture");

}



/*!

@method playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart:

@param playerViewController

 The player view controller.

@abstract Delegate can implement this method and return NO to prevent player view controller from automatically being dismissed when Picture in Picture starts.

 */

- (BOOL)playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart:(AVPlayerViewController *)playerViewController;{

    return NO;

}
/*!

@method playerViewController:restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:

@param playerViewController

 The player view controller.

@param completionHandler

 The completion handler the delegate needs to call after restore.

@abstract Delegate can implement this method to restore the user interface before Picture in Picture stops.

 */

- (void)playerViewController:(AVPlayerViewController *)playerViewController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler;{

    completionHandler(YES);

}

@end
***************
AVFoundation中的AVPlayer实现播放
 
 //读取本地视频路径
      NSString *path = [[NSBundle mainBundle] pathForResource:@"water" ofType:@"MOV"];
       //为即将播放的视频内容进行建模
       AVPlayerItem *avplayerItem = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:path]];
   //创建监听(这是一种KOV的监听模式)
       [avplayerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
       //给播放器赋值要播放的对象模型
       AVPlayer *avplayer = [AVPlayer playerWithPlayerItem:avplayerItem];
   //指定显示的Layer
       AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:avplayer];
       layer.frame = CGRectMake(0, 100, self.view.frame.size.width, 200);
       [self.view.layer addSublayer:layer];




KVO监听
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    AVPlayerItem *item = object;
    //判断监听对象的状态
    if ([keyPath isEqualToString:@"status"]) {
    
        if (item.status == AVPlayerItemStatusReadyToPlay) {//准备好的
            NSLog(@"AVPlayerItemStatusReadyToPlay");
        } else if(item.status ==AVPlayerItemStatusUnknown){//未知的状态
           NSLog(@"AVPlayerItemStatusUnknown");
        }else if(item.status ==AVPlayerItemStatusFailed){//有错误的
            NSLog(@"AVPlayerItemStatusFailed");
        }
        
    }
    
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值